在编译期间提升未定义的引用

时间:2011-11-21 19:13:21

标签: c++ windows boost mingw

我试图从文档中编译一个简单的测试程序时出现编译错误。

C:\DOCUME~1\A\LOCALS~1\Temp\ccRsXzHu.o:tester.cpp:(.text+0xa6): undefined reference to     `_imp___ZN5boost6thread4joinEv'
C:\DOCUME~1\A\LOCALS~1\Temp\ccRsXzHu.o:tester.cpp:(.text+0xb4): undefined reference to     `_imp___ZN5boost6threadD1Ev'
C:\DOCUME~1\A\LOCALS~1\Temp\ccRsXzHu.o:tester.cpp:(.text+0xcf): undefined reference to     `_imp___ZN5boost6threadD1Ev'
C:\DOCUME~1\A\LOCALS~1\Temp\ccRsXzHu.o:tester.cpp:    (.text$_ZN5boost11this_thread18interruptible_waitEy[boost::this_thread::interruptible_wait(    unsigned long long)]+0x4a): undefined reference to     `_imp___ZN5boost11this_thread18interruptible_waitEPvNS_6detail7timeoutE'
C:\DOCUME~1\A\LOCALS~1\Temp\ccRsXzHu.o:tester.cpp:     (.text$_ZN5boost6threadC1IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thre    ad_move_tIS4_EEEEPNS0_5dummyEE4typeE[boost::thread::thread<void (*)()>(void (*)(),     boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)]+0x23): undefined reference to     `_imp___ZN5boost6thread12start_threadEv'
collect2: ld returned 1 exit status

我在windows上使用mingw 4.5和g ++ 4.5.2。提升版本v1.4.8。

我希望有人可以帮我解决这个问题。

感谢。

2 个答案:

答案 0 :(得分:3)

看起来您没有链接到boost库。

Boost没有Windows,因为它不是标准库。您必须下载头文件和库,然后在项目中包含头文件并在编译时链接到库。由于您使用的是g ++,这意味着在编译命令中添加-l行。 -l行必须与您想要使用的每个特定库一起使用,您不能只指定boost目录。

This page将帮助您开始使用Windows,this page将帮助您开始使用* nix平台。

一旦你编译了boost,那么在你的例子中,你应该用

编译你的程序
g++ -o tester.exe -Lpath/to/boost/libraries/ -lboost_thread tester.c

答案 1 :(得分:0)

确保您已正确链接所有库。

如果您的线程库是静态定义的,请尝试将此行放在第一位

#define BOOST_THREAD_USE_LIB

另外,请查看this thread