转载:http://www.cnblogs.com/flywuya/archive/2010/11/30/1892524.html
使用boost::thread库时可能会碰到如下错误 (linux下 gcc4.4+boost1.40)
||=== testthread, Release ===|
obj/Release/main.o||In function `main':|
main.cpp|| undefined reference to `boost::thread::join()'|
main.cpp|| undefined reference to `boost::thread::join()'|
main.cpp|| undefined reference to `boost::thread::~thread()'|
main.cpp|| undefined reference to `boost::thread::~thread()'|
main.cpp|| undefined reference to `boost::thread::~thread()'|
main.cpp|| undefined reference to `boost::thread::~thread()'|
obj/Release/main.o||In function `boost::detail::thread_data<void (*)()>::~thread_data()':|
::~thread_data()]+0x14)||undefined reference to `boost::detail::thread_data_base::~thread_data_base()'|
obj/Release/main.o||In function `boost::detail::thread_data<void (*)()>::~thread_data()':|
::~thread_data()]+0x11)||undefined reference to `boost::detail::thread_data_base::~thread_data_base()'|
obj/Release/main.o||In function `boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)':|
::type)]+0x4b)||undefined reference to `vtable for boost::detail::thread_data_base'|
::type)]+0x166)||undefined reference to `boost::thread::start_thread()'|
::type)]+0x18a)||undefined reference to `boost::thread_resource_error::thread_resource_error()'|
::type)]+0x192)||undefined reference to `boost::thread_resource_error::~thread_resource_error()'|
::type)]+0x19a)||undefined reference to `typeinfo for boost::thread_resource_error'|
::type)]+0x1ba)||undefined reference to `boost::thread_resource_error::thread_resource_error()'|
::type)]+0x1c2)||undefined reference to `boost::thread_resource_error::~thread_resource_error()'|
::type)]+0x1ca)||undefined reference to `typeinfo for boost::thread_resource_error'|
::type)]+0x1eb)||undefined reference to `boost::thread_resource_error::thread_resource_error()'|
::type)]+0x1f6)||undefined reference to `boost::thread_resource_error::~thread_resource_error()'|
::type)]+0x1fe)||undefined reference to `typeinfo for boost::thread_resource_error'|
::type)]+0x223)||undefined reference to `boost::thread_resource_error::thread_resource_error()'|
::type)]+0x22e)||undefined reference to `boost::thread_resource_error::~thread_resource_error()'|
::type)]+0x236)||undefined reference to `typeinfo for boost::thread_resource_error'|
::type)]+0x25b)||undefined reference to `boost::thread_resource_error::thread_resource_error()'|
::type)]+0x266)||undefined reference to `boost::thread_resource_error::~thread_resource_error()'|
::type)]+0x26e)||undefined reference to `typeinfo for boost::thread_resource_error'|
]+0x8)||undefined reference to `typeinfo for boost::detail::thread_data_base'|
||=== Build finished: 26 errors, 0 warnings (0 minutes, 3 seconds) ===|
或者以下错误 (windows gcc4.5+boost1.45)
C:\Boost_1_45\include\boost-1_45\boost\thread\win32\thread_heap_alloc.hpp|59|warning: inline function 'void* boost::detail::allocate_raw_heap_memory(unsigned int)' declared as dllimport: attribute ignored|
C:\Boost_1_45\include\boost-1_45\boost\thread\win32\thread_heap_alloc.hpp|69|warning: inline function 'void boost::detail::free_raw_heap_memory(void*)' declared as dllimport: attribute ignored|
obj\Debug\main.o:main.cpp:(.text$_ZN5boost12thread_groupD1Ev[boost::thread_group::~thread_group()]+0x77)||undefined reference to `_imp___ZN5boost6threadD1Ev'|
obj\Debug\main.o:main.cpp:(.text$_ZN5boost12thread_group8join_allEv[boost::thread_group::join_all()]+0x87)||undefined reference to `_imp___ZN5boost6thread4joinEv'|
::type)]+0x58)||undefined reference to `_imp___ZN5boost6thread12start_threadEv'|
::type)]+0x55)||undefined reference to `_imp___ZN5boost6thread12start_threadEv'|
::~auto_ptr()]+0x14)||undefined reference to `_imp___ZN5boost6threadD1Ev'|
||=== Build finished: 5 errors, 2 warnings (0 minutes, 5 seconds) ===|
以上错误是由于未导入thread的静态链接库,解决方法如下
如果使用boost::thread,使用静态链接时 即编译boost库时使用选项link=static runtime-link=static
对于ubuntu下,需要在project的build options中linker settings--》other linker options中添加 -lboost_thread
对于windows下,需要在project的build options中linker settings--》link libraries中添加boost_1_45\lib\libboost_thread-mgw45-mt-1_45.a(注意,debug和release要田间对应的库文件),并修改头文件(boost_1_45\include\boost_1_45\boost\thread\detail\config.hpp)中第40行,在末尾添加宏 || defined(MINGW32),同时在build options中compiler settings中的#defines添加MINGW32或者一劳永逸地在全局设定中添加此宏(settings-->compiler and debugger-->compiler settings-->#defines中添加红MINGW32)
使用动态链接库时,即编译boost库时使用选项link=shared runtime-link=shared
对于windows下,需要在project的build options中linker settings--》link libraries中添加boost_1_45\lib\libboost_thread-mgw45-mt-1_45-dll.a(注意,debug和release要田间对应的库文件),并将此dll文件拷贝到编译的可执行程序根目录下