链接升压时出错 [英] Error in linking boost

查看:66
本文介绍了链接升压时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to boost threads and am trying to compile a simple example I found:

#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 进行编译,并得到以下错误:

I am compiling it with g++ -Wall -I $(BOOST)/include -L $(BOOST)/lib/ -lboost_system test.cpp and get these errors:

/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上使用boost的1.55版本和gcc的4.6.1版本.我想念什么?

I am using version 1.55 of boost and 4.6.1 of gcc on an Ubuntu 11.10. What am I missing?

谢谢

推荐答案

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

You might try re-ordering to put library names at the end.

我尝试了这一点(默认情况下,boost已包含在我的include和linker路径中,所以我不需要指定include和linker的位置,只需指定一些库名即可),它对我有用:

I tried this (boost is already in my default include and linker path, so I didn't need to specify the include and linker locations, just some library names), and it worked for me:

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

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

So for your example, you might want:

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

按照您最初显示的方式进行操作,我得到的错误与您一样.

Doing it the way around that you showed originally, and I got the same error as you did.

此外,通过拆分编译和链接步骤,可以使所有内容变得更加清晰(并且一旦有多个源文件,就想立即执行此操作).

Also, you can make it all clearer (and you'll be wanting to do this anyway as soon as you have more than one source file), by splitting out the compile and link steps, like this:

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

第一行的-c告诉g ++仅编译对象,而不链接.

The -c on the first line tells g++ to just compile the object, and not link.

这篇关于链接升压时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆