Boost [Thread] undefined reference to `boost::thread::thread......undefined reference to `boost::thread::thread......

刚开始用Boost的Thread,难免出现些编译,链接错误,非常的折腾人。

Boost的某些库是不需要生成,直接包含其hpp文件就能使用的,而有些酷则需要生成。

建议仔细按照其主页上的Getting Started Guide,进行设置。

下面是我使用Regex和Thread库时的体会,按照Getting Started Guide的设置,使用Regex时没出现链接问题,但是Thread却折腾了我很久。

后来发信去Boost user maillist,才得到解决。

把它写出来,希望对碰到同样问题的朋友有用,自己要是再碰到也能马上解决。

 

我用的是linux下的Boost,windows下的boost我没用过,所以我这讲的是针对linux(fedora 8), 编译器是 gcc 4.1。

 

我就直接把我的求救信和得到的指点贴出来了。是英文的,不好意思,大家看起来可能费劲点。

 

 

subject:[Thread] undefined reference to `boost::thread::thread......

 

Hello ,every one.
I got undefefined reference errors while using thread libs, I reviewd
some existing post
on this subject, but still can't solve my problem.
So I paste my problem here, I appreciate any help. Thank you.
My platform is fedora 8, gcc  4.1.2 20070925 (Red Hat 4.1.2-33).
I downloaded boost_1_37_0.tar.bz2 from www.boost.org
I followed the "Getting started guide" on the www.boost.org
I extracted the boost_1_37_0.tar.bz2 use "tar --bzip2 -xf
/usr/local/boost_1_37_0.tar.bz2";
Because I want to write multi-thread programs using boost, so I
decided to build the separately-compiled Boost libraries.
I followed the "Getting Started Guide".
I run the following cmds:
 ./configure --prefix=/usr/local/boost_1_37_0/
 make install
So, after long wait, it's done, all the librarys are build.
My /usr/local/boost_1_37_0 directory structure looks like this:
-----------------------------------------------------------------------------------
drwxr-xr-x  3 root root  4096 11-03 16:51 bin.v2
drwxr-xr-x 69 1000 1000  4096 11-02 20:50 boost
-rw-r--r--  1 1000 1000   848 06-26 23:57 boost-build.jam
-rw-r--r--  1 1000 1000   969 2006-12-02 boost.css
-rw-r--r--  1 1000 1000  6308 2005-08-12 boost.png
-rwxr-xr-x  1 1000 1000  9909 2008-04-30 configure
-rw-r--r--  1 root root     0 11-05 22:05 dir
drwxr-xr-x  4 1000 1000  4096 11-02 21:05 doc
drwxr-xr-x  3 root root  4096 11-03 16:51 include
-rw-r--r--  1 1000 1000   770 2008-02-22 index.htm
-rw-r--r--  1 1000 1000  5505 10-20 21:20 index.html
-rw-r--r--  1 1000 1000   291 2008-03-27 INSTALL
-rw-r--r--  1 1000 1000 16934 11-01 21:14 Jamroot
drwxr-xr-x  2 root root 12288 11-03 17:14 lib
drwxr-xr-x 80 1000 1000  4096 11-02 20:53 libs
-rw-r--r--  1 1000 1000  1338 2003-09-30 LICENSE_1_0.txt
-rw-r--r--  1 root root  1026 11-03 19:57 Makefile
drwxr-xr-x  4 1000 1000  4096 11-02 20:53 more
drwxr-xr-x  2 1000 1000  4096 11-02 20:49 people
-rw-r--r--  1 1000 1000  2610 2008-03-23 rst.css
drwxr-xr-x  2 1000 1000  4096 11-02 20:49 status
drwxr-xr-x 13 1000 1000  4096 11-02 20:53 tools
-rw-r--r--  1 root root   165 11-03 16:49 user-config.jam
-rw-r--r--  1 root root   113 11-03 16:47 user-config.jam.1
drwxr-xr-x  2 1000 1000  4096 11-02 20:53 wiki
----------------------------------------------------------------------------------
I test the regex library by running the program from the "Getting
Started Guide", which links the regex library.
I named the program regex.cpp.
Following is the cmd I used to compile the program.
"c++ -I /usr/local/boost_1_37_0 regex.cpp -o regex
/usr/local/boost_1_37_0/lib/libboost_regex-gcc41-mt-1_37.a"
There is no error occurs.

Then, I write another program trying to use the thread library.
I named this program thre.cpp.
Following is my program, a  very simple one:
-----------------------------------------------------------------------------
#include <boost/thread.hpp>
#include <iostream>
void hello()
{
       std::cout<<std::endl<<"hello world, I'm a thread!"<<std::endl;
}
int main()
{
       boost::thread thrd(&hello);
       thrd.join();
       return 0;
}
------------------------------------------------------------------------------------
Then I compile it use the following cmd, very similar to  the previous one
" c++ -I /usr/local/boost_1_37_0 thre.cpp -o thre
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a"
but I got the following errors:
-------------------------------------------------------------------------------------
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::set_current_thread_data(boost::detail::thread_data_base*)':
thread.cpp:(.text+0x12a): undefined reference to `pthread_setspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::(anonymous
namespace)::create_current_thread_tls_key()':
thread.cpp:(.text+0x146): undefined reference to `pthread_key_create'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::get_current_thread_data()':
thread.cpp:(.text+0x173): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::thread::start_thread()':
thread.cpp:(.text+0x648): undefined reference to `pthread_create'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::thread::detach()':
thread.cpp:(.text+0x79f): undefined reference to `pthread_detach'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `thread_proxy':
thread.cpp:(.text+0x9ce): undefined reference to `pthread_setspecific'
thread.cpp:(.text+0xa0d): undefined reference to `pthread_setspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::(anonymous
namespace)::make_external_thread_data()':
thread.cpp:(.text+0xe84): undefined reference to `pthread_setspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::find_tss_data(void const*)':
thread.cpp:(.text+0x1427): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::this_thread::restore_interruption::~restore_interruption()':
thread.cpp:(.text+0x1493): undefined reference to `pthread_getspecific'
thread.cpp:(.text+0x14b8): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::this_thread::restore_interruption::~restore_interruption()':
thread.cpp:(.text+0x14f3): undefined reference to `pthread_getspecific'
thread.cpp:(.text+0x1518): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):thread.cpp:(.text+0x155d):
more undefined references to `pthread_getspecific' follow
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::thread::join()':
thread.cpp:(.text+0x1be9): undefined reference to `pthread_join'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::set_tss_data(void const*,
boost::shared_ptr<boost::detail::tss_cleanup_function>, void*, bool)':
thread.cpp:(.text+0x1f5d): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::get_tss_data(void const*)':
thread.cpp:(.text+0x2137): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::this_thread::disable_interruption::disable_interruption()':
thread.cpp:(.text+0x2197): undefined reference to `pthread_getspecific'
thread.cpp:(.text+0x21d1): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::this_thread::disable_interruption::disable_interruption()':
thread.cpp:(.text+0x2217): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):thread.cpp:(.text+0x2251):
more undefined references to `pthread_getspecific' follow
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::thread::timed_join(boost::posix_time::ptime
const&)':
thread.cpp:(.text+0x237a): undefined reference to `pthread_join'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::condition_variable::timed_wait(boost::unique_lock<boost::mutex>&,
boost::posix_time::ptime const&)':
thread.cpp:(.text._ZN5boost18condition_variable10timed_waitERNS_11unique_lockINS_5mutexEEERKNS_10posix_time5ptimeE[boost::condition_variable::timed_wait(boost::unique_lock<boost::mutex>&,
boost::posix_time::ptime const&)]+0x26): undefined reference to
`pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(once.o):
In function `boost::detail::get_once_per_thread_epoch()':
once.cpp:(.text+0x17): undefined reference to `pthread_once'
once.cpp:(.text+0x24): undefined reference to `pthread_getspecific'
once.cpp:(.text+0x4d): undefined reference to `pthread_setspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(once.o):
In function `create_epoch_tss_key':
once.cpp:(.text+0x86): undefined reference to `pthread_key_create'
collect2: ld 返回 1
-------------------------------------------------------------------------------------
But in fact ,in the lib directory , there exist the thread libs, they are:
-------------------------------------------------------------------------------------
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt.a
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt.so
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.so
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.so.1.37.0
-------------------------------------------------------------------------------------

 

 

I also tried another cmds on these two programs, they are:
"g++ -o regex -lz regex.cpp
/usr/local/boost_1_37_0/lib/libboost_regex-gcc41-mt-1_37.a"
"g++ -o thre -lz thre.cpp
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a"

the regex cmd runs ok, but the thre cmd again failed. Got the following errors:
------------------------------------------------------------------------------------
thre.cpp:(.text+0xad): undefined reference to
`boost::thread::thread(boost::function0<void,
std::allocator<boost::function_base> > const&)'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::set_current_thread_data(boost::detail::thread_data_base*)':
thread.cpp:(.text+0x12a): undefined reference to `pthread_setspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::(anonymous
namespace)::create_current_thread_tls_key()':
thread.cpp:(.text+0x146): undefined reference to `pthread_key_create'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::get_current_thread_data()':
thread.cpp:(.text+0x173): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::thread::start_thread()':
thread.cpp:(.text+0x648): undefined reference to `pthread_create'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::thread::detach()':
thread.cpp:(.text+0x79f): undefined reference to `pthread_detach'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `thread_proxy':
thread.cpp:(.text+0x9ce): undefined reference to `pthread_setspecific'
thread.cpp:(.text+0xa0d): undefined reference to `pthread_setspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::(anonymous
namespace)::make_external_thread_data()':
thread.cpp:(.text+0xe84): undefined reference to `pthread_setspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::find_tss_data(void const*)':
thread.cpp:(.text+0x1427): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::this_thread::restore_interruption::~restore_interruption()':
thread.cpp:(.text+0x1493): undefined reference to `pthread_getspecific'
thread.cpp:(.text+0x14b8): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::this_thread::restore_interruption::~restore_interruption()':
thread.cpp:(.text+0x14f3): undefined reference to `pthread_getspecific'
thread.cpp:(.text+0x1518): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):thread.cpp:(.text+0x155d):
more undefined references to `pthread_getspecific' follow
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::thread::join()':
thread.cpp:(.text+0x1be9): undefined reference to `pthread_join'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::set_tss_data(void const*,
boost::shared_ptr<boost::detail::tss_cleanup_function>, void*, bool)':
thread.cpp:(.text+0x1f5d): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::detail::get_tss_data(void const*)':
thread.cpp:(.text+0x2137): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::this_thread::disable_interruption::disable_interruption()':
thread.cpp:(.text+0x2197): undefined reference to `pthread_getspecific'
thread.cpp:(.text+0x21d1): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::this_thread::disable_interruption::disable_interruption()':
thread.cpp:(.text+0x2217): undefined reference to `pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):thread.cpp:(.text+0x2251):
more undefined references to `pthread_getspecific' follow
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::thread::timed_join(boost::posix_time::ptime
const&)':
thread.cpp:(.text+0x237a): undefined reference to `pthread_join'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
In function `boost::condition_variable::timed_wait(boost::unique_lock<boost::mutex>&,
boost::posix_time::ptime const&)':
thread.cpp:(.text._ZN5boost18condition_variable10timed_waitERNS_11unique_lockINS_5mutexEEERKNS_10posix_time5ptimeE[boost::condition_variable::timed_wait(boost::unique_lock<boost::mutex>&,
boost::posix_time::ptime const&)]+0x26): undefined reference to
`pthread_getspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(once.o):
In function `boost::detail::get_once_per_thread_epoch()':
once.cpp:(.text+0x17): undefined reference to `pthread_once'
once.cpp:(.text+0x24): undefined reference to `pthread_getspecific'
once.cpp:(.text+0x4d): undefined reference to `pthread_setspecific'
/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(once.o):
In function `create_epoch_tss_key':
once.cpp:(.text+0x86): undefined reference to `pthread_key_create'
collect2: ld 返回 1
---------------------------------------------------------------------------------

I attatched my makefile, hope it give some cues.

Thank you for your patience reading so long a post.

 

 

下面是解答,很小的问题,但折磨了我很久,大家其实仔细看看输出错误也许就能找到症结。但我当时在赶工压力下,脑子已经不在理智,有点急,所以花了几天时间。

 

> I got undefefined reference errors while using thread libs, I reviewd
> some existing post
> on this subject, but still can't solve my problem.


> Then I compile it use the following cmd, very similar to  the previous one
> " c++ -I /usr/local/boost_1_37_0 thre.cpp -o thre
> /usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a"
> but I got the following errors:
> -------------------------------------------------------------------------------------
> /usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a(thread.o):
> In function `boost::detail::set_current_thread_data(boost::detail::thread_data_base*)':
> thread.cpp:(.text+0x12a): undefined reference to `pthread_setspecific'


This is a clue: pthread_setspecific is part of the pthread library,
not Boost.Thread. On linux, the thread library depends on the pthreads
library, so you also need -lpthread on your command line.


c++ -I /usr/local/boost_1_37_0 thre.cpp -o thre /

/usr/local/boost_1_37_0/lib/libboost_thread-gcc41-mt-1_37.a -lpthread

You may also need a "-mthread" or "-mthreads" or "-pthread" compiler
switch to enable gcc to generate multi-thread compatible code.

Anthony
--
Anthony Williams
Author of C++ Concurrency in Action | http://www.manning.com/williams

Custom Software Development | http://www.justsoftwaresolutions.co.uk
Just Software Solutions Ltd, Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK

 

 

 

That's it!

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值