轉載: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檔案拷貝到編譯的可執行程式根目錄下