提升线程错误:未定义的引用

时间:2010-08-27 13:01:25

标签: c++ boost boost-thread

#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()'

任何人都可以告诉我为什么会收到此错误吗?

7 个答案:

答案 0 :(得分:36)

使用mt标签进行编译,即-lboost_thread-mt

答案 1 :(得分:18)

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

答案 2 :(得分:17)

我有同样的问题,但是-lboost_thread-mt现已弃用,请参阅askubuntu.com上的this answer。相反,你现在想要的makefile(至少对于linux)是:

-lpthread -lboost_thread ...

Boost只是让你有责任链接到系统的线程库。

答案 3 :(得分:2)

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

答案 4 :(得分:1)

代替

g++ -pthread -lboost_thread X.cpp

试试

g++ X.cpp -pthread -lboost_thread 

答案 5 :(得分:0)

添加编译选项

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

格雷格的回答是对的!

答案 6 :(得分:0)

我有同样的错误。我用-lboost_thread

修复了它