试图静态链接Boost

Ale*_*x F 14 linux linker boost static-linking

我在Linux,Eclipse CDT,g ++和Boost库中工作.有了使用Boost线程的现有程序,我尝试静态而不是动态地链接它./ usr/local/lib目录包含以下文件:

libbost_thread.a
libbost_thread.so
libbost_thread.1.41.0
Run Code Online (Sandbox Code Playgroud)

动态链接工作:

g++  -o"MyProgram"  ./main.o   -lboost_thread
Run Code Online (Sandbox Code Playgroud)

静态链接:

g++ -static -o"MyProgram"  ./main.o   -lboost_thread
Run Code Online (Sandbox Code Playgroud)

产生大量的消息,如:

对'pthread_mutex_init'的未定义引用

如何静态链接到Boost库?

R S*_*hko 24

对于pthread_mutex_init,您希望使用-pthread选项编译/链接:

g++ -static -pthread -o"MyProgram"  ./main.o   -lboost_thread
Run Code Online (Sandbox Code Playgroud)

问题是像pthread_mutex_init这样的函数位于一个单独的库中.动态库可以包含需要单独库的事实的元数据(因此libboost_thread.so包含它需要libpthread的事实).

但静态库没有这些信息.因此,您需要在静态链接时提供对任何必需库的引用.

至于使用-pthread而不是-lpthread,它稍微优先,因为它不仅链接必要的库,而且提供应该使用的任何其他选项(例如-D_REENTRANT编译器).

  • +1表示`-lpthread`和`-pthread`之间的重要区别 (2认同)