尝试静态链接Boost [英] Trying to statically link Boost
问题描述
我正在使用Boost库在Linux,Eclipse CDT,g ++中工作.在具有使用Boost线程的现有程序的情况下,我尝试将其静态链接而不是动态链接. /usr/local/lib目录包含以下文件:
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
动态链接有效:
g++ -o"MyProgram" ./main.o -lboost_thread
静态链接:
g++ -static -o"MyProgram" ./main.o -lboost_thread
产生大量的消息,例如:
produces huge number of messages like:
对"pthread_mutex_init"的未定义引用
undefined reference to `pthread_mutex_init'
如何静态链接到Boost库?
How can I link statically to the Boost library?
推荐答案
对于pthread_mutex_init,您希望使用-pthread选项进行编译/链接:
For pthread_mutex_init, you want to compile/link with -pthread option:
g++ -static -pthread -o"MyProgram" ./main.o -lboost_thread
问题是像pthread_mutex_init这样的函数在单独的库中.动态库可以包含它需要单独的库的事实的元数据(因此libboost_thread.so包含它需要libpthread的事实).
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.
对于使用-pthread
而不是-lpthread
,它稍微更可取,因为它不仅链接了必要的库,而且还提供了应使用的任何其他选项(例如编译器中的-D_REENTRANT
).
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).
这篇关于尝试静态链接Boost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!