2010-08-27 64 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 ... 

升壓簡直給你的責任,鏈接到你的系統的線程庫。