#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()'有人能告诉我为什么会出现这个错误吗?
发布于 2012-05-07 17:46:37
使用mt标签进行编译,即-lboost_thread-mt
发布于 2016-02-12 00:54:29
我也有同样的问题,但是-lboost现在被弃用了参见askubuntu.com上的this answer。相反,您现在想要的makefile (至少在linux上)是:
-lpthread -lboost_thread ...Boost只是让你负责链接到你系统的线程库。
发布于 2010-08-27 21:25:17
许多boost库都在头文件中完全实现。Boost.thread并非如此。它似乎没有链接到boost线程库中。检查链接器搜索路径。或者,正如Stargazer712对OP的评论所说,检查安装。您应该会在lib目录中看到类似libboothread.xxx-1_nn.o的内容。如果是这样,请尝试在您的链接步骤中显式引用它(类似于-L<path_to_lib> -lboost-thread-gcc-xx-1_nn)。如果不是,那么很明显你没有一个完整的安装。
https://stackoverflow.com/questions/3584365
复制相似问题