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

问题描述 投票:22回答:6
#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()'

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

c++ boost boost-thread
6个回答
36
投票

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


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


18
投票

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

-lpthread -lboost_thread ...

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


2
投票

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


0
投票

添加编译选项

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

格雷格的回答是对的!


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.