链接Boost库在Linux中 [英] Linking Boost Library in Linux
问题描述
我想建立使用Boost的短耳一个项目,我遇到了一些麻烦。起初,我试图建立项目,没有任何额外的库,因为一切假想是在头文件。
I am trying to build a project using Boost's Asio and I am having some trouble. Initially, I tried to build the project without any additional libraries since everything is supposedly in the header files.
我试图建立类似下面这样的程序:
The program I am trying to build looks like this:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello, world!" << std::endl;
return 0;
}
可以发现这里在升压的网站上。
所以,最初我只是有:
-I /usr/include/boost_1_40_0
这导致了以下错误:
make -k all
Building target: HelloWorld
Invoking: GCC C++ Linker
g++ -o"HelloWorld" ./main.o
./main.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost_1_40_0/boost/system/error_code.hpp:205: undefined reference to `boost::system::get_system_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:206: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:211: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:212: undefined reference to `boost::system::get_generic_category()'
/usr/include/boost_1_40_0/boost/system/error_code.hpp:213: undefined reference to `boost::system::get_system_category()'
./main.o: In function `boost::asio::error::get_system_category()':
/usr/include/boost_1_40_0/boost/asio/error.hpp:218: undefined reference to `boost::system::get_system_category()'
./main.o: In function `error_code':
/usr/include/boost_1_40_0/boost/system/error_code.hpp:312: undefined reference to `boost::system::get_system_category()'
./main.o: In function `posix_tss_ptr':
/usr/include/boost_1_40_0/boost/asio/detail/posix_tss_ptr.hpp:47: undefined reference to `pthread_key_create'
./main.o: In function `~posix_tss_ptr':
/usr/include/boost_1_40_0/boost/asio/detail/posix_tss_ptr.hpp:61: undefined reference to `pthread_key_delete'
./main.o: In function `boost::asio::detail::posix_thread::join()':
/usr/include/boost_1_40_0/boost/asio/detail/posix_thread.hpp:77: undefined reference to `pthread_join'
./main.o: In function `~posix_thread':
/usr/include/boost_1_40_0/boost/asio/detail/posix_thread.hpp:69: undefined reference to `pthread_detach'
collect2: ld returned 1 exit status
make: *** [HelloWorld] Error 1
make: Target `all' not remade because of errors.
看来我所需要的系统库。所以,我也跟着在入门指南中的发现这里一>,它给了我一串位于 / usr / include目录/ boost_1_40_0 /台/ lib目录。其中是的 libboost_system.a 的。因此,我试图用编译:
It appeared that I needed the system library. So, I followed the directions on the Getting Started guide found here, which gave me a bunch of libraries located in /usr/include/boost_1_40_0/stage/lib. Among them was libboost_system.a. Thus, I attempted to compile with:
-I /usr/include/boost_1_40_0
-L /usr/include/boost_1_40_0/stage/lib
-l libboost_system
不过,我得到这个:
However, I got this:
make -k all
Building target: HelloWorld
Invoking: GCC C++ Linker
g++ -L/usr/lib -L/usr/include/boost_1_40_0/stage/lib -o"HelloWorld" ./main.o -llibboost_system
/usr/bin/ld: cannot find -llibboost_system
collect2: ld returned 1 exit status
make: *** [HelloWorld] Error 1
make: Target `all' not remade because of errors.
我不知道为什么,但它似乎无法识别库或任何我试试其他人。什么可能我会做正确?在此先感谢!
I'm not sure why, but it can't seem to identify the library or any of the others that I try. What might I be doing incorrectly? Thanks in advance!
推荐答案
修改 -llibboost_system
到 -lboost_system
。
在Linux中,LIB$ P $在图书馆前PFIX不使用时,引用说库。
In linux, the "lib" prefix in front of a library is not used when referencing said library.
这篇关于链接Boost库在Linux中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!