环境: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)