使用提升线程时编译失败

Compile failed when using boost thread

本文关键字:编译 失败 线程      更新时间:2023-10-16

这是我的代码:

#include <boost/thread.hpp>
#include <iostream>
using namespace boost; 
void task1()
{std::cout << "This is task1!" << std::endl;}
void task2()
{std::cout << "This is task2!" << std::endl;}
int main () 
{
thread thread_1 = thread(task1);
thread thread_2 = thread(task2);
thread_2.join();
thread_1.join();
return 0;
}

我编译:

g++ test.cc -o test -lboost_thread -lpthread -lboost_system

编译失败:

/tmp/ccN9cPiI.o: In function `boost::system::generic_category()':test.cc(.text._ZN5boost6system16generic_categoryEv[_ZN5boost6system16generic_categoryEv]+0x7):
undefined reference to `boost::system::detail::generic_category_instance'
collect2: error: ld returned 1 exit status

这是怎么回事? 我的系统是 Ubuntu 18.04,boost 是 1_68.0。

我刚刚尝试了你的例子,它有效。我尝试了boost_1_66_0,并在 Ubuntu 上boost_1_65_1。

实际问题是提升本身,而不是您的安装。您可以在此处检查未解决的错误:

https://github.com/boostorg/system/issues/24