2012-07-23 40 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