gpt4 book ai didi

c++ - boost同步服务器客户端编译报错

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:40 26 4
gpt4 key购买 nike

我正在尝试编译这段代码,这是一个在 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

最佳答案

您应该链接到 Boost.Thread 库。添加-lboost_thread

注意:您可能拥有它的多线程版本。然后尝试 -lboost_thread-mt

关于c++ - boost同步服务器客户端编译报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15513554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com