g++编译C++11遇到的问题:Enable multithreading to use std::thread: Operation not permitted
2017-08-15 16:44
2336 查看
[cpp] view
plain copy
#include<iostream>
#include<thread>
void hello()
{
std::cout<<"hello concurrent world\n";
}
int main()
{
std::thread t(hello);
t.join();
}
我安装了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)
在查了好久才发现问题,是编译器的问题,编译时要加上选项
因为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文件加入:
参考文章:http://stackoverflow.com/questions/19463602/compiling-multithread-code-with-g
plain copy
#include<iostream>
#include<thread>
void hello()
{
std::cout<<"hello concurrent world\n";
}
int main()
{
std::thread t(hello);
t.join();
}
我安装了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
相关文章推荐
- c++ g++运行出现Enable multithreading to use std::thread: Operation not permitted解决
- Enable multithreading to use std::thread: Operation not permitted
- what(): Enable multithreading to use std::thread: Operation not permitted
- c++11编译错误 Enable multithreading to use std::thread: Operation not permitted
- Enable multithreading to use std::thread: Operation not permitted
- linux下编译c++程序遇到 error: ‘thread’ is not a member of ‘std’的错误
- 单点登录(八)-----遇到问题-----Application Not Authorized to Use CAS
- 单点登录(八)-----遇到问题-----Application Not Authorized to Use CAS
- g++使用std::thread编译错误问题
- c++求教:我在linux下用CodeLite编译调试时出现&"warning: GDB: Failed to set controlling terminal: Operation not permitted/n"
- IDEA 编译错误:java: try-with-resources is not supported in -source 1.6 (use -source 7 or higher to enable try-with-resources)
- Docker容器使用问题:Failed to get D-Bus connection: Operation not permitted
- Maven错误 diamond operator is not supported in -source 1.5 (use -source 7 or higher to enable diamond operator)问题解决
- linux无线遇到问题Operation not possible due to RF-kill
- 编译kinfu过程中遇到的问题,CMake提示:Could not find the following Boost libraries: boost_sysytem boost_thread
- g++使用std::thread编译错误问题
- rsync: failed to set times on "/." (in ftp): Operation not permitted (1)
- cocoapods 配置私有库遇到的library not found for -l编译报错问题
- 解决问题:编译Latex遇到File ended while scanning use of \@writefile
- [mongoDB]Failed to unlink socket file /tmp/mongodb-[portnum].sock Operation not permitted