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

lal*_*lal 23 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;
}
Run Code Online (Sandbox Code Playgroud)

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

/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()'
Run Code Online (Sandbox Code Playgroud)

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

小智 37

用mt标签编译ie -lboost_thread-mt

  • 我遇到了类似的问题,这解决了!谢谢! (2认同)

gre*_*egg 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).如果没有,那么你显然没有完整的安装.

  • 为了记录,它是-lboost_thread.见http://antonym.org/2009/05/threading-with-boost---part-i-creating-threads.html (8认同)

Ala*_*lin 18

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

-lpthread -lboost_thread ...
Run Code Online (Sandbox Code Playgroud)

Boost简单地让您负责链接到系统的线程库.