繁体   English   中英

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

[英]Eclipse c++11 thread support not working

我遵循了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”“ ../main.cpp”

g ++ -pthread -std = c ++ 11 -o“沙盒” ./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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM