今天在使用boost::thread库时,
示例:
1
2 3 4 5 6 7 8 9 10 11 |
void test
(
)
{ std :: cout << "run!\n" ; } int main ( int argc , char * argv [ ] ) { boost :: thread thread1 ( &test ) ; thread1. join ( ) ; return 0 ; } |
一直编译不过,提示:
Z:/cpp/boost/t2.cpp:25: undefined reference to `_imp___ZN5boost6thread4joinEv'
Z:/cpp/boost/t2.cpp:27: undefined reference to `_imp___ZN5boost6threadD1Ev'
Z:/cpp/boost/t2.cpp:27: undefined reference to `_imp___ZN5boost6threadD1Ev'
obj\Debug\t2.o: In function `thread':
E:/boost/include/boost-1_46_1/boost/thread/detail/thread.hpp:27: undefined reference to `_imp___ZN5boost6thread12start_threadEv'
collect2: ld returned 1 exit status
开始怀疑版本boost 1.46.1编译不正确,重新编译了后问题依旧,最后查看thread/detail/config.hpp配置文件,
经测试我当前环境win32下,boost::thread库默认定义为BOOST_THREAD_USE_DLL的动态编译,这显然和我当前的静态编译方式不符,
分别参考两个参数的说明:
BOOST_THREAD_USE_LIB
For compilers supporting auto-tss cleanup
with Boost.Threads lib, use Boost.Threads lib
BOOST_THREAD_USE_DLL
For compilers not yet supporting auto-tss cleanup
with Boost.Threads lib, use Boost.Threads dll
隧在代码中预定义编译符号
...
#define BOOST_THREAD_USE_LIB
...
编译正常通过。