【发布时间】:2017-01-22 21:18:36
【问题描述】:
我有以下代码
-
文件hello.cc
static A dummyl; A:: A() { fun(); } void A::fun() { int y = 10; int z = 20; int x = y + z; } -
文件hello.h
class A { public: A a; void fun(); }; -
文件main.cc
#include <dlfcn.h> #include "hello.h" typedef void (*pf)(); int main() { void *lib; pf greet; const char * err; printf("\n Before dlopen\n"); lib = dlopen("libhello.so", RTLD_NOW | RTLD_GLOBAL); if (!lib) { exit(1); } A *a = new A ; a->fun(); dlerror(); /*first clear any previous error; redundant in this case but a useful habit*/ dlclose(lib); return 0; }
构建阶段:
- g++ -fPIC -c hello.cc
- g++ -shared -o libhello.so hello.o
- g++ -o myprog main.cc -ldl -L。 -lhello
由于我在实际情况下的共享库是 libQtCore.so ,我需要在链接器中使用 -lQtCore 来链接它,因为我不能直接使用符号,并且因为有很多函数,所以 libQtCore,实际上不建议为 libQtCore.so
由于我链接,我的静态全局变量在 dlopen 之前被初始化。链接器 g++ 是否有任何标志告诉编译器仅在 _dlopen _ 之后加载共享库?
【问题讨论】:
标签: c++ qt gcc linker shared-libraries