linux gcc使用C++11的thread库,只有几行程序,出运行时错误!

g1343jksd 2014-03-06 11:14:57
我在ubuntu13.10上面用apt-get安装了gcc4.8.1,尝试下面的小程序:

#include <thread>
#include <iostream>
#include <stdio.h>
using namespace std;
void f()
{
}
int main(int argc, char* argv[])
{
thread t(f);
t.join();
return 0;
}


编译运行,出错:
$ g++ a.cpp -std=c++11 -lpthread && ./a.out
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)
------------------------------------------
这是为什么呢? 错误在哪里? 版本信息如下:
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.8.1-10ubuntu9' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)
...全文
422 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mujiok2003 2014-03-06
  • 打赏
  • 举报
回复
http://stackoverflow.com/questions/21129735/g-4-8-1-c-threads-stdsystem-error-operation-not-permitted
JiMoKuangXiangQu 2014-03-06
  • 打赏
  • 举报
回复
这里是关于g++编译器这个bug的官方参考链接: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52681 供参考。
JiMoKuangXiangQu 2014-03-06
  • 打赏
  • 举报
回复 1
试试这样: g++ a.cpp -std=c++11 -pthread && ./a.out
mujiok2003 2014-03-06
  • 打赏
  • 举报
回复
引用
g++ a.cpp -std=c++11 -lpthread
g++ -std=c++11 a.cpp -lpthread 试试
g1343jksd 2014-03-06
  • 打赏
  • 举报
回复
引用 4 楼 mujiok2003 的回复:
http://stackoverflow.com/questions/21129735/g-4-8-1-c-threads-stdsystem-error-operation-not-permitted
这个是我想要的答案! 链接选项加上-Wl,--no-as-needed

64,684

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧