G ++找不到boost库

时间:2014-01-21 15:12:38

标签: c++ boost g++ linker-errors ld

我不擅长命令行编译。我的问题是无法编译简单的项目,这取决于Boost。以下是我尝试的日志:

$ g++ -Wall test.cpp -o main
/tmp/ccCTvBYE.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x6b): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x77): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x83): undefined reference to `boost::system::system_category()'
/tmp/ccCTvBYE.o: In function `boost::asio::error::get_system_category()':
test.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status

所以there我找到了添加-lboost_system-lboost_system-mt的说明。我得到了以下内容:

$ g++ -lboost_system -Wall test.cpp -o main                                                                                                                    
/usr/bin/ld: cannot find -lboost_system
collect2: error: ld returned 1 exit status

$ g++ -lboost_system-mt -Wall test.cpp -o main                                                                                                                 
/usr/bin/ld: cannot find -lboost_system-mt
collect2: error: ld returned 1 exit status

我试图找到boost_system库。

$ /sbin/ldconfig -p | grep boost_system
    libboost_system.so.1.53.0 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libboost_system.so.1.53.0

然后我没有成功尝试以下

$ g++ -I"/home/third_party/boost/" -L"/usr/lib/x86_64-linux-gnu/" -lboost_system -Wall test.cpp -o main
/usr/bin/ld: cannot find -lboost_system
collect2: error: ld returned 1 exit status

现在我被卡住了。如何形成编译命令?

编辑:

以下尝试也没有帮助。

ln -s /usr/lib/x86_64-linux-gnu/libboost_system.so.1.53.0 /usr/lib/x86_64-linux-gnu/libboost_system.so
ldconfig -n /usr/lib/x86_64-linux-gnu/

$ ll /usr/lib/x86_64-linux-gnu/ | grep boost_system
lrwxrwxrwx   1 root root       51 янв.  21 19:47 libboost_system.so -> /usr/lib/x86_64-linux-gnu/libboost_system.so.1.53.0
-rw-r--r--   1 root root    14536 окт.  13 07:14 libboost_system.so.1.53.0

$ g++ -I"/home/third_party/boost/" -L"/usr/lib/x86_64-linux-gnu/" -lboost_system -Wall -m64 boost_async.cpp -o main
/tmp/ccE20K2W.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x6b): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x77): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x83): undefined reference to `boost::system::system_category()'
/tmp/ccE20K2W.o: In function `boost::asio::error::get_system_category()':
test.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status

1 个答案:

答案 0 :(得分:10)

如果将链接器指令-lboost_system放在命令行的末尾,如下所示:

g++ -I"/home/third_party/boost/" -L"/usr/lib/x86_64-linux-gnu/" -Wall -m64 boost_async.cpp -o main -lboost_system

这应该可以解决问题。感谢Colin D Bennettwesley.mesquita澄清了这个答案。