2

このコードをコンパイルしようとしています。これは、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;

typedef boost::shared_ptr<ip::tcp::socket> socket_ptr;

int main()
{

    io_service service;
    void client_session(socket_ptr sock);
    ip::tcp::endpoint ep( ip::address::from_string("127.0.0.1"), 2001);
    ip::tcp::acceptor acc(service, ep);
    while (true) {
        socket_ptr sock(new ip::tcp::socket(service));
        acc.accept(*sock);
        boost::thread(boost::bind(client_session,sock));
    }

}

void client_session(socket_ptr sock)
{
    while(true) {
        char data[512];
        size_t len = sock->read_some(buffer(data));
        if (len > 0)
            write(*sock, buffer("ok", 2));
    }
}

コンパイル中にエラー...

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

/tmp/cc2mbAsP.o: In function `main':
test2.cpp:(.text+0x13d): undefined reference to `boost::thread::~thread()'
/tmp/cc2mbAsP.o: In function `boost::detail::thread_data_base::thread_data_base()':
test2.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x26): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/cc2mbAsP.o: In function `boost::thread::thread<boost::_bi::bind_t<void, void (*)(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >), boost::_bi::list1<boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > >(boost::_bi::bind_t<void, void (*)(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >), boost::_bi::list1<boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > >, boost::disable_if<boost::is_convertible<boost::_bi::bind_t<void, void (*)(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >), boost::_bi::list1<boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > >&, boost::detail::thread_move_t<boost::_bi::bind_t<void, void (*)(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >), boost::_bi::list1<boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > > >, boost::thread::dummy*>::type)':
test2.cpp:(.text._ZN5boost6threadC2INS_3_bi6bind_tIvPFvNS_10shared_ptrINS_4asio19basic_stream_socketINS5_2ip3tcpENS5_21stream_socket_serviceIS8_EEEEEEENS2_5list1INS2_5valueISC_EEEEEEEET_NS_10disable_ifINS_14is_convertibleIRSK_NS_6detail13thread_move_tISK_EEEEPNS0_5dummyEE4typeE[_ZN5boost6threadC5INS_3_bi6bind_tIvPFvNS_10shared_ptrINS_4asio19basic_stream_socketINS5_2ip3tcpENS5_21stream_socket_serviceIS8_EEEEEEENS2_5list1INS2_5valueISC_EEEEEEEET_NS_10disable_ifINS_14is_convertibleIRSK_NS_6detail13thread_move_tISK_EEEEPNS0_5dummyEE4typeE]+0x7c): undefined reference to `boost::thread::start_thread()'
/tmp/cc2mbAsP.o: In function `boost::detail::thread_data<boost::_bi::bind_t<void, void (*)(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >), boost::_bi::list1<boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > >::~thread_data()':
test2.cpp:(.text._ZN5boost6detail11thread_dataINS_3_bi6bind_tIvPFvNS_10shared_ptrINS_4asio19basic_stream_socketINS5_2ip3tcpENS5_21stream_socket_serviceIS8_EEEEEEENS2_5list1INS2_5valueISC_EEEEEEED2Ev[_ZN5boost6detail11thread_dataINS_3_bi6bind_tIvPFvNS_10shared_ptrINS_4asio19basic_stream_socketINS5_2ip3tcpENS5_21stream_socket_serviceIS8_EEEEEEENS2_5list1INS2_5valueISC_EEEEEEED5Ev]+0x3b): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
test2.cpp:(.text._ZN5boost6detail11thread_dataINS_3_bi6bind_tIvPFvNS_10shared_ptrINS_4asio19basic_stream_socketINS5_2ip3tcpENS5_21stream_socket_serviceIS8_EEEEEEENS2_5list1INS2_5valueISC_EEEEEEED2Ev[_ZN5boost6detail11thread_dataINS_3_bi6bind_tIvPFvNS_10shared_ptrINS_4asio19basic_stream_socketINS5_2ip3tcpENS5_21stream_socket_serviceIS8_EEEEEEENS2_5list1INS2_5valueISC_EEEEEEED5Ev]+0x55): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/cc2mbAsP.o:(.rodata._ZTIN5boost6detail11thread_dataINS_3_bi6bind_tIvPFvNS_10shared_ptrINS_4asio19basic_stream_socketINS5_2ip3tcpENS5_21stream_socket_serviceIS8_EEEEEEENS2_5list1INS2_5valueISC_EEEEEEEE[typeinfo for boost::detail::thread_data<boost::_bi::bind_t<void, void (*)(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > >), boost::_bi::list1<boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > >]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status

何か案は?

修繕:

g++ を使用 -o test2 test2.cpp -lboost_system -lpthread -lboost_thread-mt

4

1 に答える 1

0

Boost.Threadライブラリに対してリンクする必要があります。追加し-lboost_threadます。

:マルチスレッドバージョンを使用している可能性があります。次に、試してください-lboost_thread-mt

于 2013-03-20T01:21:58.950 に答える