[英]Eclipse c++11 thread support not working
I have followed the recommendations of other answers on SO, but still Eclipse will not build my threaded application: 我遵循了SO上其他答案的建议,但是Eclipse仍不会构建线程应用程序:
#include <thread>
#include <iostream>
void func() {
std::cout << "Hello thread!" << std::endl;
}
int main() {
std::thread t(func);
t.join();
return 0;
}
Here are the commands Eclipse is using (emphasis mine): 以下是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 ++ -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 "sandbox" ./main.o g ++ -pthread -std = c ++ 11 -o“沙盒” ./main.o
As you can see, I added -pthread and -std=c++11. 如您所见,我添加了-pthread和-std = c ++ 11。 Still, Eclipse complains at run-time: 尽管如此,Eclipse在运行时仍然抱怨:
terminate called after throwing an instance of 'std::system_error' 抛出'std :: system_error'实例后调用终止
what(): Enable multithreading to use std::thread: Operation not permitted what():启用多线程以使用std :: thread:不允许进行操作
Also, building manually fails: 此外,手动构建失败:
$ 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
What else can I do? 我还可以做些什么?
The answer was to add the following to two locations: 答案是将以下内容添加到两个位置:
-Wl,--no-as-needed
It should be added to: 它应该添加到:
1) Project Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Miscellaneous -> Other flags 1)项目属性-> C / C ++构建->设置->工具设置-> GCC C ++编译器->其他->其他标志
Example: -c -fmessage-length=0 -pthread -std=c++11 -Wl,--no-as-needed
示例: -c -fmessage-length=0 -pthread -std=c++11 -Wl,--no-as-needed
2) Project Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Miscellaneous -> Linker flags 2)项目属性-> C / C ++构建->设置->工具设置-> GCC C ++链接器->其他->链接器标志
Example: -pthread -std=c++11 -Wl,--no-as-needed
示例: -pthread -std=c++11 -Wl,--no-as-needed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.