链接提升时出错

时间:2014-05-10 10:17:13

标签: c++ boost boost-thread

我是新手来提升线程,我正在尝试编译一个我发现的简单示例:

#include <iostream>
#include <boost/thread.hpp>   
#include <boost/date_time.hpp>       

void workerFunc()  
{  
    boost::posix_time::seconds workTime(3);          
    std::cout << "Worker: running" << std::endl;    

    // Pretend to do something useful... 
    boost::this_thread::sleep(workTime);          
    std::cout << "Worker: finished" << std::endl;  
}    

int main(int argc, char* argv[])  
{  
    std::cout << "main: startup" << std::endl;          
    boost::thread workerThread(workerFunc);    

    std::cout << "main: waiting for thread" << std::endl;          
    workerThread.join();    

    std::cout << "main: done" << std::endl;          
    return 0;  
}

我正在使用g++ -Wall -I $(BOOST)/include -L $(BOOST)/lib/ -lboost_system test.cpp进行编译并收到以下错误:

/tmp/ccp180QW.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x160): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x16c): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x178): undefined reference to `boost::system::system_category()'
/tmp/ccp180QW.o: In function `boost::thread_exception::thread_exception(int, char const*)':
test.cpp:(.text._ZN5boost16thread_exceptionC2EiPKc[_ZN5boost16thread_exceptionC5EiPKc]+0x14): undefined reference to `boost::system::system_category()'
/tmp/ccp180QW.o: In function `boost::detail::thread_data_base::thread_data_base()':
test.cpp:(.text._ZN5boost6detail16thread_data_baseC2Ev[_ZN5boost6detail16thread_data_baseC5Ev]+0x24): undefined reference to `vtable for boost::detail::thread_data_base'
/tmp/ccp180QW.o: In function `boost::this_thread::sleep(boost::posix_time::ptime const&)':
test.cpp:(.text._ZN5boost11this_thread5sleepERKNS_10posix_time5ptimeE[boost::this_thread::sleep(boost::posix_time::ptime const&)]+0x3e): undefined reference to `boost::this_thread::hiden::sleep_until(timespec const&)'
/tmp/ccp180QW.o: In function `boost::thread::start_thread()':
test.cpp:(.text._ZN5boost6thread12start_threadEv[boost::thread::start_thread()]+0x15): undefined reference to `boost::thread::start_thread_noexcept()'
/tmp/ccp180QW.o: In function `boost::thread::~thread()':
test.cpp:(.text._ZN5boost6threadD2Ev[_ZN5boost6threadD5Ev]+0x15): undefined reference to `boost::thread::detach()'
/tmp/ccp180QW.o: In function `boost::thread::get_id() const':
test.cpp:(.text._ZNK5boost6thread6get_idEv[boost::thread::get_id() const]+0x18): undefined reference to `boost::thread::native_handle()'
/tmp/ccp180QW.o: In function `boost::thread::join()':
test.cpp:(.text._ZN5boost6thread4joinEv[boost::thread::join()]+0x6d): undefined reference to `boost::thread::join_noexcept()'
/tmp/ccp180QW.o: In function `boost::detail::thread_data<void (*)()>::~thread_data()':
test.cpp:(.text._ZN5boost6detail11thread_dataIPFvvEED2Ev[_ZN5boost6detail11thread_dataIPFvvEED5Ev]+0x1f): undefined reference to `boost::detail::thread_data_base::~thread_data_base()'
/tmp/ccp180QW.o:(.rodata._ZTIN5boost6detail11thread_dataIPFvvEEE[typeinfo for boost::detail::thread_data<void (*)()>]+0x10): undefined reference to `typeinfo for boost::detail::thread_data_base'
collect2: ld returned 1 exit status

我在Ubuntu 11.10上使用1.55版本的增强版和4.6.1版本的gcc。我错过了什么?

由于

1 个答案:

答案 0 :(得分:4)

您可以尝试重新排序以将库名称放在最后。

我试过这个(boost已经在我的默认包含和链接器路径中,所以我不需要指定包含和链接器位置,只需要一些库名称),它对我有用:

g++ -Wall test.cpp -o test -lboost_system -lboost_thread

因此,对于您的示例,您可能需要:

g++ -Wall -I$(BOOST)/include test.cpp -o test -L$(BOOST)/lib -lboost_system -lboost_thread

按照你原来展示的方式做到这一点,我得到了和你一样的错误。

此外,您可以更清楚地了解(并且只要您拥有多个源文件,您就会想要执行此操作),通过拆分编译和链接步骤,如下所示:

g++ -c -Wall -o test.o test.cpp
g++ test.o -o test -lboost_system -lboost_thread

第一行的-c告诉g ++只编译对象,而不是链接。

相关问题