共享库中静态变量的构造函数
Constructor of static variable inside shared library
我很好奇在共享库中实例化的静态变量是否被实例化了。
首先,这是代码:
AClass.h
class A {
public:
A(int x) { cout << "A constructor called." << endl; }
};
MyLib.cpp
#include "AClass.h"
static A StaticVariableA(50);
所以,通过这段代码加上一个 Main.cpp 文件(除了一个空的 main 函数什么都没有),我想让构造函数被初始化调用MyLib.cpp.
中的变量
我将MyLib.cpp编译成一个共享库,并以多种方式将主程序链接到它,但都没有成功。鉴于这两个文件已经编译成目标文件:
(first compiled MyLib.cpp to a shared library)
$ g++ -O0 -fPIC -shared -Wl,-soname,libMyLib.so -o libMyLib.so MyLib.o
(Tried to link the two files)
$ g++ -O0 Main.o -rdynamic libMyLib.so -Wl,-rpath,./
$ g++ -O0 Main.o -L . -Wl,-rpath,./ -lMyLib
到现在还没有成功,请问:这可能吗?如果不可能,为什么?如何在共享库中处理静态变量? (我主要对Linux机器感兴趣)
(我知道我不应该像这样使用共享库。但我只是想知道它做了什么,这是行不通的)
奇怪的是,this code 有效。将库 GMLOSStFeatEx 链接到 /tools/Dummy.cpp
时,这是一个用 /src/FeatureExtractor/
个文件编译的共享库。它调用RegisterStaticFeature
.
类型静态变量的构造函数
编辑
好像@Pupsik说的,正在优化中。但是,我上面链接的代码没有得到优化。它也没有任何被外部代码调用或引用的函数。
我在this question it suggests the use of the linker flag -no-as-needed
, which works. But as it is possible to see below, there is no such flag when compiling the code中提到过。
/usr/bin/c++ -O0 -g -I/usr/local/include -fPIC -fvisibility-inlines-hidden
-Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual
-Wno-missing-field-initializers -pedantic -Wno-long-long
-Wno-maybe-uninitialized -Wnon-virtual-dtor -Wno-comment -std=c++11 -g
-fno-exceptions -fno-rtti -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS
-D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -L/usr/local/lib
CMakeFiles/Dummy.dir/DummyMain.cpp.o -o Dummy -rdynamic ../src
/FeatureExtractor/libGMLOSStFeatEx.so ../src/FeatureExtractor
/libGMLOSDynFeatEx.so ../src/Support/libGMLOSCfgPrinter.so
-lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMX86Desc -lLLVMX86Info
-lLLVMMCDisassembler -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMCodeGen
-lLLVMScalarOpts -lLLVMProfileData -lLLVMInstCombine -lLLVMInstrumentation
-lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMIRReader -lLLVMAsmParser
-lLLVMTransformUtils -lLLVMipa -lLLVMMCJIT -lLLVMExecutionEngine -lLLVMTarget
-lLLVMAnalysis -lLLVMRuntimeDyld -lLLVMObject -lLLVMMCParser -lLLVMBitReader
-lLLVMMC -lLLVMCore -lLLVMSupport -lpapi -lrt -ldl -ltinfo -lpthread -lz -lm
-Wl,-rpath,/path-to-gmlos/build/src/FeatureExtractor:/path-to-gmlos/build/src/Support
所以,事实证明@Pupsik 是完全正确的!顺便说一句,谢谢。
正如他所说,库正在优化中:
Maybe your MyLib.cpp was optimized out by the compiler since no references found to that library? Try to add some dummy function in the MyLib.cpp and call it from main.cpp
我引用的 github 上的代码没有得到优化,因为确实有一个函数在该库中被主函数调用。
我找到的解决方案是使用链接器标志 -as-needed
。它无论如何都链接库,因此不会从二进制文件中取出。
那么,这就把事情搞清楚了。
再次感谢。
我很好奇在共享库中实例化的静态变量是否被实例化了。 首先,这是代码:
AClass.h
class A {
public:
A(int x) { cout << "A constructor called." << endl; }
};
MyLib.cpp
#include "AClass.h"
static A StaticVariableA(50);
所以,通过这段代码加上一个 Main.cpp 文件(除了一个空的 main 函数什么都没有),我想让构造函数被初始化调用MyLib.cpp.
中的变量我将MyLib.cpp编译成一个共享库,并以多种方式将主程序链接到它,但都没有成功。鉴于这两个文件已经编译成目标文件:
(first compiled MyLib.cpp to a shared library)
$ g++ -O0 -fPIC -shared -Wl,-soname,libMyLib.so -o libMyLib.so MyLib.o
(Tried to link the two files)
$ g++ -O0 Main.o -rdynamic libMyLib.so -Wl,-rpath,./
$ g++ -O0 Main.o -L . -Wl,-rpath,./ -lMyLib
到现在还没有成功,请问:这可能吗?如果不可能,为什么?如何在共享库中处理静态变量? (我主要对Linux机器感兴趣)
(我知道我不应该像这样使用共享库。但我只是想知道它做了什么,这是行不通的)
奇怪的是,this code 有效。将库 GMLOSStFeatEx 链接到 /tools/Dummy.cpp
时,这是一个用 /src/FeatureExtractor/
个文件编译的共享库。它调用RegisterStaticFeature
.
编辑
好像@Pupsik说的,正在优化中。但是,我上面链接的代码没有得到优化。它也没有任何被外部代码调用或引用的函数。
我在this question it suggests the use of the linker flag -no-as-needed
, which works. But as it is possible to see below, there is no such flag when compiling the code中提到过。
/usr/bin/c++ -O0 -g -I/usr/local/include -fPIC -fvisibility-inlines-hidden
-Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual
-Wno-missing-field-initializers -pedantic -Wno-long-long
-Wno-maybe-uninitialized -Wnon-virtual-dtor -Wno-comment -std=c++11 -g
-fno-exceptions -fno-rtti -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS
-D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -L/usr/local/lib
CMakeFiles/Dummy.dir/DummyMain.cpp.o -o Dummy -rdynamic ../src
/FeatureExtractor/libGMLOSStFeatEx.so ../src/FeatureExtractor
/libGMLOSDynFeatEx.so ../src/Support/libGMLOSCfgPrinter.so
-lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMX86Desc -lLLVMX86Info
-lLLVMMCDisassembler -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMCodeGen
-lLLVMScalarOpts -lLLVMProfileData -lLLVMInstCombine -lLLVMInstrumentation
-lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMIRReader -lLLVMAsmParser
-lLLVMTransformUtils -lLLVMipa -lLLVMMCJIT -lLLVMExecutionEngine -lLLVMTarget
-lLLVMAnalysis -lLLVMRuntimeDyld -lLLVMObject -lLLVMMCParser -lLLVMBitReader
-lLLVMMC -lLLVMCore -lLLVMSupport -lpapi -lrt -ldl -ltinfo -lpthread -lz -lm
-Wl,-rpath,/path-to-gmlos/build/src/FeatureExtractor:/path-to-gmlos/build/src/Support
所以,事实证明@Pupsik 是完全正确的!顺便说一句,谢谢。
正如他所说,库正在优化中:
Maybe your MyLib.cpp was optimized out by the compiler since no references found to that library? Try to add some dummy function in the MyLib.cpp and call it from main.cpp
我引用的 github 上的代码没有得到优化,因为确实有一个函数在该库中被主函数调用。
我找到的解决方案是使用链接器标志 -as-needed
。它无论如何都链接库,因此不会从二进制文件中取出。
那么,这就把事情搞清楚了。 再次感谢。