无法构建静态boost-filesystem

时间:2012-02-28 23:03:22

标签: boost

我有这个示例代码。

#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;

int main()
{
  return 0;
}

可以使用g++ -lboost_system-mt -lboost_filesystem-mt 1.cpp

正确构建

但如果我添加-static,则会抱怨:

/tmp/cc1JEbRQ.o: In function `__static_initialization_and_destruction_0(int, int)':
1.cpp:(.text+0xb0): undefined reference to `boost::system::get_system_category()'
1.cpp:(.text+0xba): undefined reference to `boost::system::get_generic_category()'
1.cpp:(.text+0xc4): undefined reference to `boost::system::get_generic_category()'
1.cpp:(.text+0xce): undefined reference to `boost::system::get_generic_category()'
1.cpp:(.text+0xd8): undefined reference to `boost::system::get_system_category()'
collect2: ld returned 1 exit status

我该如何解决?感谢

1 个答案:

答案 0 :(得分:1)

您可能需要撤消库的顺序才能使静态链接成功,因为boost_filesystem取决于boost_system

g++ 1.cpp -static -lboost_filesystem-mt -lboost_system-mt

这是因为运行时链接器执行拓扑依赖性排序以正确的顺序加载共享库,而静态链接不会这样做。

或者,您可以强制静态链接在库列表上执行多次传递,以尝试解析剩余的未定义符号:

man ld

   -( archives -)
   --start-group archives --end-group
       The archives should be a list of archive files.  They may be either
       explicit file names, or -l options.

       The specified archives are searched repeatedly until no new
       undefined references are created.  Normally, an archive is searched
       only once in the order that it is specified on the command line.
       If a symbol in that archive is needed to resolve an undefined
       symbol referred to by an object in an archive that appears later on
       the command line, the linker would not be able to resolve that
       reference.  By grouping the archives, they all be searched
       repeatedly until all possible references are resolved.

       Using this option has a significant performance cost.  It is best
       to use it only when there are unavoidable circular references
       between two or more archives.

e.g:

g++ 1.cpp -static -Wl,--start-group -lboost_system-mt -lboost_filesystem-mt -Wl,--end-group
相关问题