我正在尝试编译此代码,这是一个在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
任何想法?
FIXED:
使用g ++ -o test2 test2.cpp -lboost_system -lpthread -lboost_thread-mt
答案 0 :(得分:0)
您应该链接到Boost.Thread
库。添加-lboost_thread
。
注意:您可能有多线程版本。然后尝试-lboost_thread-mt
。