Eclipse c++11 线程支持不起作用



我已经遵循了关于SO的其他答案的建议,但Eclipse仍然不会构建我的线程应用程序:

#include <thread>
#include <iostream>
void func() {
  std::cout << "Hello thread!" << std::endl;
}
int main() {
  std::thread t(func);
  t.join();
  return 0;
}

以下是 Eclipse 正在使用的命令(强调我的):

g++ -O0 -g3 -Wall -c -fmessage-length=0 -pthread -std=c++11 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../主.cpp"

g++ -pthread -std=c++11 -o "sandbox" ./main.o

如您所见,我添加了 -pthread 和 -std=c++11。 尽管如此,Eclipse在运行时抱怨:

在抛出"std::system_error"
实例后终止调用 what(): 启用多线程以使用 std::thread: 操作不 允许

此外,手动构建会失败:

 $ g++ main.cpp -o main.out -pthread -std=c++11
 $ ./main.out 
 terminate called after throwing an instance of 'std::system_error'
   what():  Enable multithreading to use std::thread: Operation not permitted
Aborted

我还能做什么?

答案是将以下内容添加到两个位置:

-Wl,--no-as-needed

应将其添加到:

1) 项目属性 -> C/C++ 构建 ->设置 -> 工具设置 -> GCC C++编译器 -> 杂项 -> 其他标志

示例:-c -fmessage-length=0 -pthread -std=c++11 -Wl,--no-as-needed

2) 项目属性 -> C/C++ 生成 ->设置 -> 工具设置 -> GCC C++链接器 -> 杂项 -> 链接器标志

示例:-pthread -std=c++11 -Wl,--no-as-needed

相关内容

最新更新