undefined reference to `boost::system::generic_category()‘

4 篇文章 0 订阅
在尝试使用Boost.Asio进行开发时,遇到'undefined reference to `boost::system::generic_category()'的错误。该问题源于缺少libboost_system.so和libboost_pthread.so库。解决方法是在Ubuntu 16.04环境下,为Qt5.7项目添加这两个库。通过引用相关博客文章,提供了解决此问题的详细步骤。
摘要由CSDN通过智能技术生成

在学习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博客

结尾

只为记录,只为分享! 愿所写能对你有所帮助。不忘记点个赞,谢谢~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值