相关疑难解决方法(0)

c ++ linux双重破坏静态变量.链接符号重叠

环境:linux x64,编译器gcc 4.x

项目有以下结构:

static library "slib"
-- inside this library, there is static object "sobj"

dynamic library "dlib"
-- links statically "slib"

executable "exe":
-- links "slib" statically
-- links "dlib" dynamically
Run Code Online (Sandbox Code Playgroud)

在节目结束时,"sobj"被毁坏两次.这种行为是预期的,但是它在相同的内存地址被破坏了两次,即在析构函数中也是"this" - 因此存在双重破坏问题.我认为这是由于一些符号重叠.

这场冲突的解决方案是什么?也许一些链接选项?


这是测试用例:


main_exe.cpp

#include <cstdlib>

#include "static_lib.h"
#include "dynamic_lib.h"

int main(int argc, char *argv[])
{
    stat_useStatic();
    din_useStatic();
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

static_lib.h

#ifndef STATIC_LIB_H
#define STATIC_LIB_H

#include <cstdio>

void stat_useStatic();
struct CTest
{
    CTest(): status(isAlive)
    {
        printf("CTest() this=%d\n",this);
    }
    ~CTest()
    {
        printf("~CTest() this=%d, %s\n",this,status==isAlive?"is Alive":"is …
Run Code Online (Sandbox Code Playgroud)

c++ linux dynamic-linking static-linking

17
推荐指数
2
解决办法
3538
查看次数

标签 统计

c++ ×1

dynamic-linking ×1

linux ×1

static-linking ×1