在学习boost::asio时,第一次使用,编译出现问题:
代码如下:
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <iostream>
void handle1(const boost::system::error_code &ec)
{
std::cout << "handle1" << "5 s" << std::endl;
}
void handle2(const boost::system::error_code &ec)
{
std::cout << "handle2" << "5 s" << std::endl;
}
boost::asio::io_service _io_server_;
void fun()
{
_io_server_.run();
}
int main(int argc, char *argv[])
{
boost::asio::deadline_timer time1_(_io_server_,boost::posix_time::seconds(5));
time1_.async_wait(handle1);
boost::asio::deadline_timer time2_(_io_server_,boost::posix_time::seconds(5));
time2_.async_wait(handle2);
boost::thread thread1_(fun);
boost::thread thread2_(fun);
thread1_.join();
thread2_.join();
return 0;
}
编译时打印的问题如下:
main.o: In function `__static_initialization_and_destruction_0(int, int)':
/usr/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
main.o: In function `boost::system::error_code::error_code()':
/usr/include/boost/system/error_code.hpp:322: undefined reference to `boost::system::system_category()'
main.o: In function `boost::asio::error::get_system_category()':
/usr/include/boost/asio/error.hpp:230: undefined reference to `boost::system::system_category()'
main.o: In function `boost::thread_exception::thread_exception(int, char const*)':
/usr/include/boost/thread/exceptions.hpp:51: undefined reference to `boost::system::system_category()'
main.o: In function `boost::detail::thread_data_base::thread_data_base()':
/usr/include/boost/thread/pthread/thread_data.hpp:152: undefined reference to `vtable for boost::detail::thread_data_base'
main.o: In function `boost::thread::start_thread()':
/usr/include/boost/thread/detail/thread.hpp:179: undefined reference to `boost::thread::start_thread_noexcept()'
main.o: In function `boost::thread::~thread()':
/usr/include/boost/thread/detail/thread.hpp:254: undefined reference to `boost::thread::detach()'
main.o: In function `boost::thread::get_id() const':
/usr/include/boost/thread/detail/thread.hpp:741: undefined reference to `boost::thread::native_handle()'
main.o: In function `boost::thread::join()':
/usr/include/boost/thread/detail/thread.hpp:767: undefined reference to `boost::thread::join_noexcept()'
main.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
/usr/include/boost/thread/detail/thread.hpp:90: undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
main.o:(.data.rel.ro._ZTIN5boost6detail11thread_dataIPFvvEEE[_ZTIN5boost6detail11thread_dataIPFvvEEE]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
系统环境
ubuntu16.04 Qt5.7
解决方法
缺少必要的库文件:libboost_system.so, libboost_pthread.so
只需要添加以上两个库即可
-lboost_system
-lboost_thread
参考
undefined reference to `boost::system::generic_category()问题-CSDN博客
undefined reference to `vtable for boost::detail::thread_data_base'问题-CSDN博客
结尾
只为记录,只为分享! 愿所写能对你有所帮助。不忘记点个赞,谢谢~