C++ Boost 线程错误:未定义的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3584365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:11:59  来源:igfitidea点击:

Boost thread error: undefined reference

c++boostboost-thread

提问by lal

#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;
}

I ran tried to compile this program, and got these errors:

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

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

Can any one tell me why i am getting this error?

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

回答by ankita

compile with mt tag i.e -lboost_thread-mt

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

回答by AlaskaJoslin

I had the same question, but -lboost_thread-mt is now deprecated see this answeron askubuntu.com. Instead what you now want in your makefile (at least for linux) is:

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

-lpthread -lboost_thread ...

Boost has simply given you the responsibility to link to your system's thread library.

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

回答by gregg

Many boost libraries are fully implemented in header files. Boost.thread is not. It seems that it is not linking in the boost thread library. Check your linker search paths. Or, as the Stargazer712's comment on the OP says, check the installation. You should see something like libboost_thread-gcc-xxx-1_nn.o in your lib directory. If so, try referencing it explicitly in your link step (something like -L<path_to_lib> -lboost-thread-gcc-xx-1_nn). If not, then you apparently don't have a complete installation.

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

回答by Janos Erzinger

I had a similar problem with centos 6.5 when compiling povray 3.7 and this solved it - just add -lboost_thread-mtin your Makefile.

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

回答by Kevin Zhao

add compile option

添加编译选项

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

gregg's answer is right!

gregg 的答案是正确的!

回答by Carlosio

I had the same error. I fixed it compiling with -lboost_thread

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