c++双重删除文件范围变量,链接问题?

发布于 2024-12-15 09:20:02 字数 2347 浏览 1 评论 0原文

我使用的是 RHEL 6,gcc 版本为 4.1.2。

我面临着与这篇文章中描述的完全相同的问题。唯一的区别是我面临文件范围变量的这个问题。 (在类外部但在文件中声明的变量,以便可以跨两到三个函数访问它。即使我已在堆栈上声明,该变量也会被双重删除

) com/questions/6714046/c-linux-double-destruction-of-static-variable-linking-symbols-overlap">c++ linux 静态变量的双重销毁。链接符号重叠

在我的应用程序中,我有类似的情况。

static library "slib"
-- inside this library, there is static object "sobj"
dynamic library "dlib"
-- links staticly "slib"
executable "exe":
-- links "slib" staticly
-- links "dlib" dynamicly

我怀疑,我也面临着同样的问题。为了验证,我需要提出 g++ 命令,它将以上述方式构建文件。我已尝试执行以下命令,但它不会重现上述行为。

g++ -Wall -c static_lib.cpp
ar -cvq libtests.a static_lib.o

g++ -Wall -fPIC -c dynamic_lib.cpp 
g++ -shared -Wl,-soname,libtestd.so.1 -o libtestd.so.1.0 dynamic_lib.o -L. libtests.a
ln -s libtestd.so.1.0 libtestd.so

g++ -Wall -I. -L. main_exe.cpp -ltestd -ltests -o main_prog

我不确定我是否正确链接了文件或以所需的方式链接了文件。但它并没有产生我想观察到的缺陷。例如,我的可执行版本输出以下数据。

user:~/problem$ ./main_prog
CTest() this=134520880
use
use
~CTest() this=134520880, is Alive

使用 cmake 生成的 make 文件输出以下数据。

user:~/problem$ ./main_exe 
CTest() this=134520880
CTest() this=134520880
use
use
~CTest() this=134520880, is Alive
~CTest() this=134520880, is Dead

您能帮忙达到同样的效果吗?我尝试运行 makefile -n 来打印命令,但它不会打印所有命令并在构建动态库后停止。

任何意见都将受到赞赏。 更新: 我使用“make VERBOSE=1”并观察到执行的命令。我不知道为什么使用 -rdynamic 但我们在应用程序中使用相同的。请在下面找到正在执行的命令。

/usr/bin/c++    -fPIC -c static_lib.cpp
/usr/bin/ar cr libstatic_lib.a static_lib.o
/usr/bin/ranlib libstatic_lib.a
/usr/bin/c++ -fPIC -c dynamic_lib.cpp
/usr/bin/c++  -fPIC   -shared -Wl,-soname,libdynamic_lib.so -o libdynamic_lib.so dynamic_lib.o libstatic_lib.a
/usr/bin/c++    -fPIC -c main_exe.cpp
/usr/bin/c++ main_exe.o -o main_exe -rdynamic libstatic_lib.a libdynamic_lib.so -Wl,-rpath,/home/amey/c_examples/cmake

此外,nm 测试表明该符号具有不同的值。

desktop:~/c_examples/cmake$ nm libdynamic_lib.so |grep _ZN5CTest4testE
0000000000201048 B _ZN5CTest4testE
desktop:~/c_examples/cmake$ nm main_exe |grep _ZN5CTest4testE
0000000000602048 B _ZN5CTest4testE
desktop:~/c_examples/cmake$ nm libstatic_lib.a |grep _ZN5CTest4testE
0000000000000000 B _ZN5CTest4testE

那么有两个主要问题困扰着我? 1)为什么有两个构造函数?即使有两个构造函数为什么它们指向同一个对象 2)是否存在编译顺序问题?

I am on RHEL 6 with gcc version 4.1.2.

I am facing exact same problem as described in this post. Only difference is I am facing this problem for file scope variable. (The variable declared outside the class but in the file, so that it case be accessible across two to three functions. And this variable is getting double deleted even if I have declared on stack)

c++ linux double destruction of static variable. linking symbols overlap

In my application, I have similar situation.

static library "slib"
-- inside this library, there is static object "sobj"
dynamic library "dlib"
-- links staticly "slib"
executable "exe":
-- links "slib" staticly
-- links "dlib" dynamicly

I suspect, I am also facing same problem. To verify, I need to come up with g++ command which will build the files in above manner. I have tried executing following commands but it does not reproduce the said behavior.

g++ -Wall -c static_lib.cpp
ar -cvq libtests.a static_lib.o

g++ -Wall -fPIC -c dynamic_lib.cpp 
g++ -shared -Wl,-soname,libtestd.so.1 -o libtestd.so.1.0 dynamic_lib.o -L. libtests.a
ln -s libtestd.so.1.0 libtestd.so

g++ -Wall -I. -L. main_exe.cpp -ltestd -ltests -o main_prog

I am not sure If I have linked the files correctly or in required mannger. But It is not producing the defect which I want to observe. For example, my version of executable outputs following data.

user:~/problem$ ./main_prog
CTest() this=134520880
use
use
~CTest() this=134520880, is Alive

Where as make file generated using cmake, outputs following data.

user:~/problem$ ./main_exe 
CTest() this=134520880
CTest() this=134520880
use
use
~CTest() this=134520880, is Alive
~CTest() this=134520880, is Dead

Could you please help to achieve the same effect. I have tried running makefile -n to print commands but it does not print all commands and stops after building dynamic library.

Any inputs will be apprciated.
Update:
I used 'make VERBOSE=1' and observed followed commands to get executed. I don't know why -rdynamic is used but We use the same in our application. Please find commands getting executed below.

/usr/bin/c++    -fPIC -c static_lib.cpp
/usr/bin/ar cr libstatic_lib.a static_lib.o
/usr/bin/ranlib libstatic_lib.a
/usr/bin/c++ -fPIC -c dynamic_lib.cpp
/usr/bin/c++  -fPIC   -shared -Wl,-soname,libdynamic_lib.so -o libdynamic_lib.so dynamic_lib.o libstatic_lib.a
/usr/bin/c++    -fPIC -c main_exe.cpp
/usr/bin/c++ main_exe.o -o main_exe -rdynamic libstatic_lib.a libdynamic_lib.so -Wl,-rpath,/home/amey/c_examples/cmake

Additionally, nm tests revealed that symbol has different values.

desktop:~/c_examples/cmake$ nm libdynamic_lib.so |grep _ZN5CTest4testE
0000000000201048 B _ZN5CTest4testE
desktop:~/c_examples/cmake$ nm main_exe |grep _ZN5CTest4testE
0000000000602048 B _ZN5CTest4testE
desktop:~/c_examples/cmake$ nm libstatic_lib.a |grep _ZN5CTest4testE
0000000000000000 B _ZN5CTest4testE

So two major questions are hauling in my head?
1) Why there are two constructors? Even if there are two constructors why they are pointing to same object
2) Is there any compilation sequence problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-12-22 09:20:02

要获取 cmake 执行的命令列表,请运行 make clean && make -DVERBOSE=1 或在环境中设置 VERBOSE,希望有助于重复问题。要回答为什么变量被双重初始化/销毁,数据太少了。

To get a list of commands which are executed by cmake, run make clean && make -DVERBOSE=1 or set VERBOSE in the environement, hope that helps to repeat the problem. To answer why the variable is doubly initialized/destroyed, there's too little data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文