我是靠谱客的博主 腼腆夏天,这篇文章主要介绍g++编译C++11遇到的问题:Enable multithreading to use std::thread: Operation not permitted,现在分享给大家,希望可以做个参考。

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<thread>  
  3. void hello()  
  4. {  
  5.     std::cout<<"hello concurrent worldn";  
  6. }  
  7. int main()  
  8. {  
  9.     std::thread t(hello);  
  10.     t.join();  
  11. }  
我安装了g++-4.8.5版本编译器,支持C++11,使用命令行g++-4.8 -std=c++11 -lpthread *.cpp编译后,得到的可执行文件在运行时遇到错误:

terminate called after throwing an instance of 'std::system_error'

  what():  Enable multithreading to use std::thread: Operation not permitted

Aborted (core dumped)

在查了好久才发现问题,是编译器的问题,编译时要加上选项

-Wl,--no-as-needed

因为gcc4.6以后对于ld自动加上了as-needed选项。所以编译选项应该变成:
g++ -Wl,--no-as-needed -std=c++11 -pthread main.cpp

或者

g++ -std=c++11 -o main -Wl,--no-as-needed main.cpp -lpthread
这样就没有问题了!

如果是使用qtcreator的话,则需要在.pro文件加入:

LIBS += -pthread
QMAKE_CXXFLAGS += -pthread
QMAKE_CXXFLAGS += -std=c++11

参考文章:http://stackoverflow.com/questions/19463602/compiling-multithread-code-with-g

最后

以上就是腼腆夏天最近收集整理的关于g++编译C++11遇到的问题:Enable multithreading to use std::thread: Operation not permitted的全部内容,更多相关g++编译C++11遇到的问题:Enable内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(112)

评论列表共有 0 条评论

立即
投稿
返回
顶部