链接共享库的依赖关系

时间:2015-06-23 19:39:05

标签: c++ linker

我创建了一个我希望其他人使用的库。

编译我的库:

 /usr/bin/g++ -fPIC -shared -Wl,-soname,libMYLIB.so [inputs] -lboost_system -lboost_thread

编译二进制文件:

/usr/bin/g++ myTest.cpp -lMYLIB -lboost_system

我希望这一行只有:

/usr/bin/g++ myTest.cpp -lMYLIB

如何避免以后必须指定我的库依赖项?我在寻找链接器或编译器中的哪个标志?

1 个答案:

答案 0 :(得分:1)

有一个链接器选项(我的意思是output链接器http://linux.die.net/man/1/ld) - unresolved-symbols = ignore-all或-unresolved-symbols = ignore-in-object-files:

ld
     

创建动态二进制文件时,已知所有共享文件   它应该引用的库包含在链接器中   命令行。

这就是一个例子。我有一个库Determine how to handle unresolved symbols. There are four possible values for method: * ignore-all Do not report any unresolved symbols. * report-all Report all unresolved symbols. This is the default. * ignore-in-object-files Report unresolved symbols that are contained in shared libraries, but ignore them if they come from regular object files. * ignore-in-shared-libs Report unresolved symbols that come from regular object files, but ignore them if they come from shared libraries. This can be useful 和一个应用libmylib.so

首先我构建了库:

main

当我构建应用程序但我没有在命令行上添加-lmylib时。通常它会导致错误$ g++ -fpic -shared mylib.cpp -o libmylib.so ,但由于我在命令行中添加了Unresolved external symbols,因此我没有错误:

-Wl,--unresolved-symbols=ignore-in-object-files

然后我运行我的程序:

$ g++ -fpic -g main.cpp -Wl,--unresolved-symbols=ignore-in-object-files  -Wl,-rpath,.

它没有按预期工作,但后来我使用了LD_PRELOAD:

$ ./a.out 
./a.out: symbol lookup error: ./a.out: undefined symbol: _Z7my_funcd

因此,使用LD_PRELOAD可以正常工作

相关问题