2010-08-27 50 views
21
#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()' 

任何一个可以告诉我为什么我收到这个错误?

+0

听起来升压未正确安装在系统上 – riwalk 2010-08-27 13:04:45

+0

但我的 '的#include <升压/算法/ string.hpp> 的#include 的#include <升压/ thread.hpp> 使用命名空间std; 使用命名空间提升; int main() { \t string str1; \t cin >> str1; \t // string str1(“hello world!”); \t to_upper(str1); \t cout << str1; return 0; } '完美运行 – lal 2010-08-27 13:08:58

+5

我发现它需要在ubuntu上安装libboost-thread包 – lal 2010-08-27 13:22:08

回答

17

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

+5

为了记录,它是-lboost_thread。请参阅http://antonym.org/2009/05/threading-with-boost---part-i-creating-threads.html – JRG 2011-07-16 14:28:33

+1

@Josh:感谢您的意见。另见http://www.boost.org/doc/libs/1_47_0/more/getting_started/windows.html#library-naming和http://www.boost。组织/ DOC /库/ 1_47_0 /多/ getting_started/UNIX的variants.html#链接你的程序到一个升压库。 – gregg 2011-07-21 13:58:24

0

添加编译选项

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

格雷格的答案是正确的!

35

与MT标签编译即-lboost_thread-mt

+2

我有一个类似的问题,这解决了它! 谢谢! – 2012-07-20 15:04:05

+1

你救了我的一天! – ducin 2012-12-19 08:14:37

+0

或'-lboost_thread-mgwXX-mt-N_NN'其中XX是您的mgw版本,通常只有前两个(4.7.1 => 47),而Ns是您的增强版本,同样是前两个(boost_1_55_0 => 1_55),所以最后一个可能看起来像_____ ::::::'-lboost_thread-mgw47-mt-1_55' – 2014-10-16 07:07:03

2

我在CentOS的povray编译时3.7 6.5类似的问题,这解决了这个问题 - 只是在你Makefile添加-lboost_thread-mt

12

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

-lpthread -lboost_thread ... 

升压简直给你的责任,链接到你的系统的线程库。