g++编译boost::asio时错误(boost service)

source:http://blog.sina.com.cn/s/blog_6e4643130100x0tj.html


提前已经编译好了boost库经测试没有问题。
代码:
  service.cpp                                                                        
#include <iostream>
#include <boost/asio.hpp>
using namespace std;
using boost::asio::ip::tcp;

int main ()
{
    try
    {
    //
        boost::asio::io_service ioservice;
        tcp::acceptoraccept(ioservice,tcp::endpoint(tcp::v4(),6688));
        while(true)
        {
            tcp::socket sock(ioservice);
            accept.accept(sock);
            cout<<sock.remote_endpoint().address()<<endl;
            sock.write_some(boost::asio::buffer("hello pig"));
        }
    }
    catch(exception&e)
    {
        cerr<<e.what()<<endl;
    }

    return 0;
}

编译:
g++ service.cpp

错误:
/tmp/ccSU1pc4.o: In function`__static_initialization_and_destruction_0(int, int)':
service.cpp:(.text+0x22f): undefined reference to`boost::system::generic_category()'
service.cpp:(.text+0x239): undefined reference to`boost::system::generic_category()'
service.cpp:(.text+0x243): undefined reference to`boost::system::system_category()'
/tmp/ccSU1pc4.o: In function`boost::system::error_code::error_code()':
service.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x10):undefined reference to `boost::system::system_category()'
/tmp/ccSU1pc4.o: In function`boost::asio::error::get_system_category()':
service.cpp:(.text._ZN5boost4asio5error19get _system_categoryEv[boost::asio::error::get_system_category()]+0x7):undefined reference to `boost::system::system_category()'
/tmp/ccSU1pc4.o: In function`boost::asio::detail::posix_tss_ptr_create(unsignedint&)':
service.cpp:(.text._ZN5boost4asio6detail20po six_tss_ptr_createERj[boost::asio::detail::posix_tss_ptr_create(unsignedint&)]+0x15): undefined reference to`pthread_key_create'
/tmp/ccSU1pc4.o: In function`boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::task_io_service>::context>::~posix_tss_ptr()':
service.cpp:(.text._ZN5boost4asio6detail13po six_tss_ptrINS1_10call_stackINS1_15task_io_serviceEE7contextEED2Ev[_ZN5boost4asio6detail13po six_tss_ptrINS1_10call_stackINS1_15task_io_serviceEE7contextEED5Ev]+0xf):undefined reference to `pthread_key_delete'
/tmp/ccSU1pc4.o: In function`boost::asio::detail::posix_tss_ptr<boost::asio::detail::call_stack<boost::asio::detail::strand_service::strand_impl>::context>::~posix_tss_ptr()':
service.cpp:(.text._ZN5boost4asio6detail13po six_tss_ptrINS1_10call_stackINS1_14strand_service11strand_implEE7contextEED2Ev[_ZN5boost4asio6detail13po six_tss_ptrINS1_10call_stackINS1_14strand_service11strand_implEE7contextEED5Ev]+0xf):undefined reference to `pthread_key_delete'
collect2: ld 返回 1

重新编译:
g++ -o service service.cpp -L -lboost_thread -lboost_system




版权声明:本文为yan420523原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。