问题
今天使用std::thread的时候,编译正常后运行却报了这样的错误:
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++ test.cpp -std=c++11 -Wl,–no-as-needed -lpthread
这样才是正常的。 而最开始我把他们追在后面了。笑死。也就是说-lpthread要在选项后面