2012-07-23 71 views
3

我試圖在我的項目中遇到錯誤後,使用C++在Xcode(4.2)上運行一個小測試程序。Xcode上Boost的線程組

#include <boost/thread.hpp> 
#include <boost/bind.hpp>  

int main (int argc, const char * argv[]) 
{ 
    boost::thread_group tg; 
    return 0; 
} 

但整個程序構建失敗,輸出錯誤:

Undefined symbols for architecture x86_64: 
    "boost::thread::~thread()", referenced from: 
     boost::thread_group::~thread_group()in main.o 
ld: symbol(s) not found for architecture x86_64 
collect2: ld returned 1 exit status 

然後我試圖使用

thread_group * tg = new thread_group(); 

其編譯沒有問題,直到我試圖調用

tg->join_all(); 

其中編譯器o utputs錯誤,如:

Undefined symbols for architecture x86_64: 
    "boost::detail::get_current_thread_data()", referenced from: 
     boost::detail::interruption_checker::interruption_checker(_opaque_pthread_mutex_t*, _opaque_pthread_cond_t*)in main.o 
    "boost::this_thread::interruption_point()", referenced from: 
     boost::condition_variable::wait(boost::unique_lock<boost::mutex>&) in main.o 
    "boost::this_thread::disable_interruption::disable_interruption()", referenced from: 
     boost::shared_mutex::lock_shared()  in main.o 
    "boost::this_thread::disable_interruption::~disable_interruption()", referenced from: 
     boost::shared_mutex::lock_shared()  in main.o 
    "boost::thread::join()", referenced from: 
     boost::thread_group::join_all()  in main.o 
ld: symbol(s) not found for architecture x86_64 
collect2: ld returned 1 exit status 

有誰知道如何解決這些問題?我一直在使用BOOST_FOREACH等其他函數,沒有任何問題。但在嘗試使用線程時遇到這些問題。

我需要:

  • 指定我的 '其它鏈接器標記' 的標誌嗎?
  • 重新安裝boost還是某種?目前我正在使用Boost 1.49.0,使用自制軟件安裝(即sudo brew install boost)

或者是否還有其他需要包含的特定配置?

+0

'boost_thread'需要顯式鏈接。至於'BOOST_FOREACH',這是一個宏,並由預處理器擴展。 – chrisaycock 2012-07-23 17:04:34

+0

@chrisaycock那麼,即使我已經添加了-lboost_thread,它仍然會給出相同的錯誤。 – 2012-07-23 17:14:20

回答

4

boost線程庫有一個需要鏈接的實際庫對象(-lboost_thread或有時-lboost_thread-mt)。

+0

我在我的「其他鏈接器標誌」下添加了-lboost_thread,但它仍輸出相同的錯誤。 – 2012-07-23 17:15:07

+0

@JohnSmith你確定鏈接器可以找到庫嗎?您可能還需要設置庫搜索路徑以及... – trojanfoe 2012-07-23 17:17:22

+0

是的,我正確設置了包含路徑和庫搜索路徑。但仍然是同樣的錯誤。我在其他地方閱讀了關於使用--universal標誌重新安裝它的原因是什麼? – 2012-07-23 17:20:29