尝试静态链接 Boost
我正在 Linux、Eclipse CDT、g++ 和 Boost 库中工作。对于使用 Boost 线程的现有程序,我尝试静态链接它而不是动态链接它。 /usr/local/lib 目录包含以下文件:
libbost_thread.a
libbost_thread.so
libbost_thread.1.41.0
动态链接有效:
g++ -o"MyProgram" ./main.o -lboost_thread
静态链接:
g++ -static -o"MyProgram" ./main.o -lboost_thread
产生大量消息,例如:
对“pthread_mutex_init”的未定义引用
如何静态链接到 Boost 库?
I am working in Linux, Eclipse CDT, g++, with Boost library. Having existing program which uses Boost thread, I try to link it statically instead of dynamically. /usr/local/lib directory contains the following files:
libbost_thread.a
libbost_thread.so
libbost_thread.1.41.0
Dynamic linking works:
g++ -o"MyProgram" ./main.o -lboost_thread
Static linking:
g++ -static -o"MyProgram" ./main.o -lboost_thread
produces huge number of messages like:
undefined reference to `pthread_mutex_init'
How can I link statically to the Boost library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于 pthread_mutex_init,您希望使用 -pthread 选项进行编译/链接:
问题是像 pthread_mutex_init 这样的函数位于单独的库中。动态库可以包含它需要单独库的元数据(因此 libboost_thread.so 包含它需要 libpthread 的事实)。
但静态库没有这些信息。因此,当您静态链接时,您需要提供对任何必要库的引用。
至于使用
-pthread
而不是-lpthread
,它稍微好一点,因为它不仅链接必要的库,而且提供了应该使用的任何其他选项(例如>-D_REENTRANT
到编译器)。For pthread_mutex_init, you want to compile/link with -pthread option:
The problem is that functions like pthread_mutex_init are in a separate library. Dynamic libraries can include the metadata for the fact that it needs the separate library (so libboost_thread.so includes the fact that it needs libpthread).
But static libraries don't have that information. So you need to provide the reference to any necessary libraries when you link statically.
As for using
-pthread
instead of-lpthread
, it's slightly preferable because it not only links the necessary library, but provides any other options that should be used (such a-D_REENTRANT
to the compiler).尝试将
-lpthread
添加到您的调用中。Try adding
-lpthread
to your invocation.在 Linux 上,动态库可能会自动依赖于其他动态库,因此当您链接它时,您可以免费获得其他库。静态链接时,没有这样的系统,您必须手动指定其他库。
On Linux a dynamic library may automatically depend on other dynamic libraries so that when you link it, you get the other libraries for free. When linking statically, there is no such system and you have to specify the other libraries manually.