boost库学习:多线程编程报错:对‘boost::thread::xxx()’未定义的引用

最近学习boost多线程编程,简单的一个示例:

#include <boost/thread.hpp>                                       
#include <iostream>

void wait(int seconds)
{
  boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}

void thread()
{
  for (int i = 0; i < 5; ++i)
  {
    wait(1);
    std::cout << i << std::endl;
  }
}

int main()
{
  boost::thread t(thread);
  t.join();
}

编译:

g++ -ggdb3 -o test main.cpp

报错如下:

/tmp/ccDDgmyQ.o:在函数‘boost::pthread::cond_init(pthread_cond_t&)’中:
/usr/local/include/boost/thread/pthread/pthread_helpers.hpp:28:对‘pthread_condattr_setclock’未定义的引用
/tmp/ccDDgmyQ.o:在函数‘boost::detail::thread_data_base::thread_data_base()’中:
/usr/local/include/boost/thread/pthread/thread_data.hpp:159:对‘vtable for boost::detail::thread_data_base’未定义的引用
/tmp/ccDDgmyQ.o:在函数‘boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)’中:
/usr/local/include/boost/thread/pthread/thread_data.hpp:204:对‘boost::detail::get_current_thread_data()’未定义的引用
/tmp/ccDDgmyQ.o:在函数‘boost::thread::start_thread()’中:
/usr/local/include/boost/thread/detail/thread.hpp:179:对‘boost::thread::start_thread_noexcept()’未定义的引用
/tmp/ccDDgmyQ.o:在函数‘boost::thread::~thread()’中:
/usr/local/include/boost/thread/detail/thread.hpp:254:对‘boost::thread::detach()’未定义的引用
/tmp/ccDDgmyQ.o:在函数‘boost::thread::get_id() const’中:
/usr/local/include/boost/thread/detail/thread.hpp:719:对‘boost::thread::native_handle()’未定义的引用
/tmp/ccDDgmyQ.o:在函数‘boost::thread::join()’中:
/usr/local/include/boost/thread/detail/thread.hpp:745:对‘boost::thread::join_noexcept()’未定义的引用
/tmp/ccDDgmyQ.o:在函数‘boost::condition_variable::wait(boost::unique_lock<boost::mutex>&)’中:
/usr/local/include/boost/thread/pthread/condition_variable.hpp:94:对‘boost::this_thread::interruption_point()’未定义的引用
/tmp/ccDDgmyQ.o:在函数‘boost::condition_variable::do_wait_until(boost::unique_lock<boost::mutex>&, boost::detail::mono_platform_timepoint const&)’中:
/usr/local/include/boost/thread/pthread/condition_variable.hpp:137:对‘boost::this_thread::interruption_point()’未定义的引用
/tmp/ccDDgmyQ.o:在函数‘boost::detail::thread_data<void (*)()>::~thread_data()’中:
/usr/local/include/boost/thread/detail/thread.hpp:91:对‘boost::detail::thread_data_base::~thread_data_base()’未定义的引用
/tmp/ccDDgmyQ.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[_ZTIN5boost6detail11thread_dataIPFvvEEE]+0x10):对‘typeinfo for boost::detail::thread_data_base’未定义的引用
collect2: error: ld returned 1 exit status

看到最后的collect2: error: ld returned 1 exit status,猜测应该是库链接的问题。

到 /usr/local/lib 查看boost相关的库,搜索带 thread 字眼的,有 libboost_thread.a 和 libboost_thread.so,于是改成:

g++ -ggdb3 -o test main.cpp -lboost_thread

还有报错:

/usr/bin/ld: /tmp/ccy7VvkP.o: undefined reference to symbol 'pthread_condattr_setclock@@GLIBC_2.3.3'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

看来还是库链接问题,从错误提示 libpthread.so.0,再百度一下,原来boost库链接必须指定POSIX线程库,否则无法找到pthread函数库的函数实现,于是改成:

g++ -ggdb3 -o test main.cpp -lboost_thread -lpthread

就ok了~

 

参考:

Boost 编译及boost::thread静态链接使用

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值