-1

我是 boost::asio 库的新手,在尝试这个基本的同步客户端时,它给出了这个错误

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>

using namespace std;


using namespace boost::asio;

int main()
{

    io_service service;
    ip::tcp::endpoint ep( ip::address::from_string("127.0.0.1"), 80);
    ip::tcp::socket sock(service);
    sock.connect(ep);


}

编译时出错,

[root@kris server]# g++ -o test1 test1.cpp
tmp/ccnLbhhW.o: In function `__static_initialization_and_destruction_0(int, int)':
test1.cpp:(.text+0x11b): undefined reference to `boost::system::get_system_category()'
test1.cpp:(.text+0x127): undefined reference to `boost::system::get_generic_category()'
test1.cpp:(.text+0x133): undefined reference to `boost::system::get_generic_category()'
test1.cpp:(.text+0x13f): undefined reference to `boost::system::get_generic_category()'
test1.cpp:(.text+0x14b): undefined reference to `boost::system::get_system_category()'
/tmp/ccnLbhhW.o: In function `boost::system::error_code::error_code()':
test1.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::get_system_category()'
/tmp/ccnLbhhW.o: In function `boost::asio::error::get_system_category()':
test1.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[boost::asio::error::get_system_category()]+0x5): undefined reference to `boost::system::get_system_category()'
/tmp/ccnLbhhW.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
test1.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference to `pthread_detach'
/tmp/ccnLbhhW.o: In function `boost::asio::detail::posix_thread::join()':
test1.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[boost::asio::detail::posix_thread::join()]+0x2b): undefined reference to `pthread_join'
/tmp/ccnLbhhW.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::~posix_tss_ptr()':
test1.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEED2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEED5Ev]+0x15): undefined reference to `pthread_key_delete'
/tmp/ccnLbhhW.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::posix_tss_ptr()':
test1.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEC2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEC5Ev]+0x28): undefined reference to `pthread_key_create'
collect2: ld returned 1 exit status

我在 centos 6.3 上使用以下库

boost-thread-1.41.0-11.el6_1.2.x86_64
boost-serialization-1.41.0-11.el6_1.2.x86_64
boost-test-1.41.0-11.el6_1.2.x86_64
boost-devel-1.41.0-11.el6_1.2.x86_64
boost-system-1.41.0-11.el6_1.2.x86_64
boost-wave-1.41.0-11.el6_1.2.x86_64
boost-iostreams-1.41.0-11.el6_1.2.x86_64
boost-filesystem-1.41.0-11.el6_1.2.x86_64
boost-python-1.41.0-11.el6_1.2.x86_64
boost-regex-1.41.0-11.el6_1.2.x86_64
boost-program-options-1.41.0-11.el6_1.2.x86_64
boost-date-time-1.41.0-11.el6_1.2.x86_64
boost-signals-1.41.0-11.el6_1.2.x86_64
boost-graph-1.41.0-11.el6_1.2.x86_64
boost-1.41.0-11.el6_1.2.x86_64

有任何想法吗?

更新:

与运行

 [root@kris server]# g++ -o test1 test1.cpp -lboost_system

产生此错误:

    /tmp/ccwHGnkt.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
    test1.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference to `pthread_detach'
    /tmp/ccwHGnkt.o: In function `boost::asio::detail::posix_thread::join()':
    test1.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[boost::asio::detail::posix_thread::join()]+0x2b): undefined reference to `pthread_join'
    /tmp/ccwHGnkt.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::~posix_tss_ptr()':
    test1.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEED2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEED5Ev]+0x15): undefined reference to `pthread_key_delete'
    /tmp/ccwHGnkt.o: In function `boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service<boost::asio::detail::epoll_reactor<false> > >::context>::posix_tss_ptr()':
    test1.cpp:(.text._ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEC2Ev[_ZN5boost4asio6detail13posix_tss_ptrINS1_10call_stackINS1_15task_io_serviceINS1_13epoll_reactorILb0EEEEEE7contextEEC5Ev]+0x28): undefined reference to `pthread_key_create'
    collect2: ld returned 1 exit status
4

2 回答 2

2

您必须在命令行中添加-lboost_system和,如下所示:-lpthreadg++

g++ -o test1 test1.cpp -lboost_system -lpthread
于 2013-03-18T20:24:26.927 回答
0

您声称您正在“使用”所有这些库,但您的g++行似乎不包含它们。

您必须告诉链接器应该将哪些目标文件和库链接在一起。

于 2013-03-18T20:25:10.150 回答