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

void hello()
{
  std::cout <<
    "Hello world, I'm a thread!"
    << std::endl;
}

int main(int argc, char* argv[])
{
  boost::thread thrd(&hello);
  thrd.join();
  return 0;
}

我跑了试图编译这个程序,并得到了这些错误:

/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to
   `boost::thread_resource_error::thread_resource_error()'
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to 
   `boost::thread_resource_error::~thread_resource_error()'
/usr/include/boost/thread/pthread/mutex.hpp:40: undefined reference to 
   `typeinfo for boost::thread_resource_error'
./src/thread.o: In function `condition_variable':
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33: 
  undefined reference to `boost::thread_resource_error::thread_resource_error()'
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33: 
  undefined reference to `boost::thread_resource_error::~thread_resource_error()'
/usr/include/boost/thread/pthread/condition_variable_fwd.hpp:33: \
  undefined reference to `typeinfo for boost::thread_resource_error'
./src/thread.o: In function `thread_data_base':
/usr/include/boost/thread/pthread/thread_data.hpp:54: 
  undefined reference to `vtable for boost::detail::thread_data_base'
./src/thread.o: In function `thread<void (*)()>':
/usr/include/boost/thread/detail/thread.hpp:188: 
  undefined reference to `boost::thread::start_thread()'
./src/thread.o: In function `~thread_data':
/usr/include/boost/thread/detail/thread.hpp:40: 
  undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/usr/include/boost/thread/detail/thread.hpp:40: undefined reference to 
  `boost::detail::thread_data_base::~thread_data_base()'

谁能告诉我为什么我会收到这个错误?

4

7 回答 7

38

用 mt 标签编译,即-lboost_thread-mt

于 2012-05-07T09:46:37.333 回答
24

我有同样的问题,但 -lboost_thread-mt 现在已弃用,请参阅askubuntu.com 上的此答案。相反,您现在在 makefile 中想要的(至少对于 linux 而言)是:

-lpthread -lboost_thread ...

Boost 只是让您负责链接到系统的线程库。

于 2016-02-11T16:54:28.787 回答
19

许多 boost 库完全在头文件中实现。Boost.thread 不是。似乎它没有在 boost 线程库中链接。检查您的链接器搜索路径。或者,正如 Stargazer712 对 OP 的评论所说,检查安装。您应该在 lib 目录中看到类似 libboost_thread-gcc-xxx-1_nn.o 的内容。如果是这样,请尝试在链接步骤中明确引用它(类似于-L<path_to_lib> -lboost-thread-gcc-xx-1_nn)。如果没有,那么您显然没有完整的安装。

于 2010-08-27T13:25:16.917 回答
3

代替

g++ -pthread -lboost_thread X.cpp

尝试

g++ X.cpp -pthread -lboost_thread 
于 2021-03-17T14:18:06.900 回答
2

在编译 povray 3.7 时,我遇到了与 centos 6.5 类似的问题,这解决了它 - 只需添加-lboost_thread-mt你的Makefile.

于 2015-01-19T16:16:47.630 回答
0

添加编译选项

-L<path_to_lib> -lboost-thread-gcc-xx-1_nn

格雷格的回答是对的!

于 2011-10-13T07:07:14.573 回答
0

我有同样的错误。我修复了它用 -lboost_thread 编译

于 2019-04-14T03:23:29.887 回答