我正在关注这个关于提升线程的初学者教程:http: //www.codeproject.com/Articles/279053/How-to-get-started-using-Boost-threads
他们提供的这个样本一切都很好:
#define BOOST_THREAD_USE_LIB
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << std::endl;
// Pretend to do something useful...
boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
但后来我决定在没有睡眠功能的情况下尝试一下。所以我注释掉了这些行。
#define BOOST_THREAD_USE_LIB
#include <iostream>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
void workerFunc()
{
//boost::posix_time::seconds workTime(3);
std::cout << "Worker: running" << std::endl;
// Pretend to do something useful...
//boost::this_thread::sleep(workTime);
std::cout << "Worker: finished" << std::endl;
}
int main(int argc, char* argv[])
{
std::cout << "main: startup" << std::endl;
boost::thread workerThread(workerFunc);
std::cout << "main: waiting for thread" << std::endl;
workerThread.join();
std::cout << "main: done" << std::endl;
return 0;
}
我开始收到以下编译错误:
)]+0x40)||对`_gmtime32'的未定义引用|
我已经做了很多窥探,试图弄清楚这意味着什么,以及为什么只有在我删除这两行时才会发生这种情况。截至目前,我倾向于它与某种标题有关,例如 time.h,我必须包含(尽管我已经尝试过)。
我正在使用一个相当奇怪的设置。带有 mingw/gcc 的代码::块。我按照代码块教程使用命令行参数自己编译了 boost 库: http ://wiki.codeblocks.org/index.php ?title=BoostWindowsQuickRef
一切似乎都可以正常工作,但我想我可能错误地构建了库。
我会更深入地挖掘,但错误的文件名不是标准的,并且列为“)] + 0x40)”。我不确定这意味着什么——它可能是某种文件位置地址吗?
更多信息:
Windows XP 32 bit
CodeBlocks 10.05
MingW GCC 4.4.3
Boost 1_53_0
构建日志:
链接控制台可执行文件:bin\Debug\Bjarne_Strousup_Samples.exe ....\CodeBlocks\lib\libboost_thread-mgw44-mt-1_53.a(thread.o):thread.cpp:(.text$_ZN5boost9date_time6c_time6gmtimeEPKlP2tm[boost::date_time: :c_time::gmtime(long const*, tm*)]+0x40): undefined reference to `_gmtime32' collect2: ld returned 1 exit status 进程以状态 1(0 分 6 秒)终止 1 个错误,0 个警告
命令行尝试:
C:\CodeBlocks Tests\BoostExamples>g++ main.cpp -lboost_thread -lboost_system -lb
oost_chrono
main.cpp:5:28: error: boost/thread.hpp: No such file or directory
main.cpp:6:31: error: boost/date_time.hpp: No such file or directory
main.cpp: In function 'int main()':
main.cpp:21: error: 'boost' has not been declared
main.cpp:21: error: expected ';' before 'workerThread'
main.cpp:24: error: 'workerThread' was not declared in this scope
C:\CodeBlocks Tests\BoostExamples>
链接器设置:
我认为错误在 c_time.hpp 的这一行:
static std::tm* gmtime(const std::time_t* t, std::tm* result)