如何解决boost多线程编译错误的简单程序
How to resolve compilation errors for boost multithreading simple program
我在电脑中编译boost集成源代码时遇到问题
OS: CentOs 6.3
Boost version: 1.41
Boost installation header file directory: /usr/include/boost/
IDE: code::block
编译程序:
#include <boost/thread.hpp>
#include <iostream>
using namespace std;
void ThreadFunction()
{
int counter = 0;
for(;;)
{
std::cout << "thread iteration " << ++counter << " Press Enter to stop" << std::endl;
try
{
// Sleep and check for interrupt.
// To check for interrupt without sleep,
// use boost::this_thread::interruption_point()
// which also throws boost::thread_interrupted
boost::this_thread::sleep(boost::posix_time::milliseconds(500));
}
catch(boost::thread_interrupted&)
{
std::cout << "Thread is stopped" << std::endl;
return;
}
}
}
int main()
{
// Start thread
boost::thread t(&ThreadFunction);
// Wait for Enter
char ch;
std::cin.get(ch);
// Ask thread to stop
t.interrupt();
// Join - wait when thread actually exits
t.join();
std::cout << "main: thread ended" << std::endl;
return 0;
}
错误输出:
Build: Debug in testPro (compiler: GNU GCC Compiler) ===|
obj/Debug/main.o||In function `main':|
<path to code>/main.cpp|40|undefined reference to `boost::thread::interrupt()'|
<path to code>/main.cpp|43|undefined reference to `boost::thread::join()'|
<path to code>/main.cpp|47|undefined reference to `boost::thread::~thread()'|
<path to code>/main.cpp|47|undefined reference to `boost::thread::~thread()'|
obj/Debug/main.o||In function `thread_data_base':|
/usr/include/boost/thread/pthread/thread_data.hpp|65|undefined reference to `vtable for boost::detail::thread_data_base'|
obj/Debug/main.o||In function `void boost::this_thread::sleep<boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000l> >(boost::date_time::subsecond_duration<boost::posix_time::time_duration, 1000l> const&)':|
/usr/include/boost/thread/pthread/thread_data.hpp|122|undefined reference to `boost::this_thread::sleep(boost::posix_time::ptime const&)'|
obj/Debug/main.o||In function `thread<void (*)()>':|
/usr/include/boost/thread/detail/thread.hpp|191|undefined reference to `boost::thread::start_thread()'|
obj/Debug/main.o||In function `~thread_data':|
/usr/include/boost/thread/detail/thread.hpp|40|undefined reference to `boost::detail::thread_data_base::~thread_data_base()'|
]+0x10)||undefined reference to `typeinfo for boost::detail::thread_data_base'|
||=== Build failed: 9 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
在错误控制台中,编译器找不到boost库中声明的方法。我尝试了几个示例应用程序,但结果是一样的。请帮助我找出问题,或建议我检查升压安装状态的方法。
您需要使用boost_thread库编译代码-lboost_thread
例如
gcc test.c -o test -lboost_thread
相关文章:
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- std::is_base_of表示ctor编译错误
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- Qt Cmake 错误编译"GuiSupportQt not found"
- Opengl 精度转换错误编译错误 E0415
- 库将ARM架构错误编译为架构X64
- RT 音频 Mac 错误 g++ 编译错误
- 错误编译Boost.log
- 错误编译QT创建者 / QT窗口小部件示例
- 错误编译MIPS32
- Visual Studio 2013 中的错误(编译和运行代码)
- 链接错误编译qt项目在visual 2010
- 无法用模板错误编译nsgmls
- 奇怪的错误.编译失败
- 如果有人调用c++中的方法,则强制错误(编译时)
- 来自autoconf测试的错误编译命令
- 时间限制超出错误C++编译
- SFML 2.3 和 CodeBlocks 错误编译