报错大致如下:
main.cpp:(.text+0x49): undefined reference to boost::system::generic_category()'
boost::system::generic_category()’
main.cpp:(.text+0x53): undefined reference to
main.cpp:(.text+0x5d): undefined reference to `boost::system::system_category()’
当时搞了很久,各种库各种一通链接,始终没解决问题
解决方案:
“boost/filesystem.hpp” 这个文件依赖于boost_system,编译的时候,需要 -lboost_system选项,但是你的编译选项里头已经指定该选项,为什么还会出现链接错误呢。我也纳闷了半天,后来发现,原来是-l选项的顺序问题。在g++的man帮助信息里面,我发现了下面的话:
引用
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.
**- 这段话的意思是说例如编译的命令为 g++ foo.o -lz bar.o -o a.out,则编译其会按照从左到右的顺序搜索各个模块的符号表,先搜索foo.o,然后搜索z库,在搜索z库时,因为已经扫描了foo.o,所以如果foo.o引用到了z库中的函数,那么这个函数就会被加载;搜索完z库之后再搜索bar.o,如果bar.o用到了z库中未被加载的函数,那么这个函数不会再次被加载(因为z库已经被扫描过了,不会再次被扫描),所以在链接的时候就出现了找不到符号的问题。
- 解决这个问题的方法是调整各个模块在编译命令中的顺序。**
所以,解决方案就是用下面的命令来编译:
引用
g++ main.cpp -lboost_system -lboost_filesystem