免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 6775 | 回复: 3
打印 上一主题 下一主题

[C++] [结贴]linux gcc使用C++11的thread库,只有几行程序,出运行时错误! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-03-06 11:15 |只看该作者 |倒序浏览
本帖最后由 iwqueri 于 2014-03-06 15:14 编辑

我在ubuntu13.10上面用apt-get安装了gcc4.8.1,尝试下面的小程序:

  1. #include <thread>
  2. #include <iostream>
  3. #include <stdio.h>
  4. using namespace std;
  5. void f()
  6. {
  7. }
  8. int main(int argc, char* argv[])
  9. {
  10.       thread t(f);
  11.       t.join();
  12.       return 0;
  13. }
复制代码
编译运行,出错:
$ 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)

论坛徽章:
4
双子座
日期:2014-08-28 10:08:002015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:58:112015年亚洲杯之阿联酋
日期:2015-03-13 03:25:15
2 [报告]
发表于 2014-03-06 13:36 |只看该作者
试一下这个
g++ a.cpp -std=c++11  -pthread && ./a.out

论坛徽章:
0
3 [报告]
发表于 2014-03-06 14:56 |只看该作者
weishuo1999 发表于 2014-03-06 13:36
试一下这个
g++ a.cpp -std=c++11  -pthread && ./a.out


也还是不行:

$ g++ a.cpp -std=c++11 -pthread && ./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)

这到底是个什么错误?

论坛徽章:
0
4 [报告]
发表于 2014-03-06 15:14 |只看该作者
找到答案了!

http://stackoverflow.com/questio ... ation-not-permitted

gcc4.6以后对于ld自动加上了as-needed选项。所以编译选项应该变成:
g++ -Wl,--no-as-needed -std=c++11 -pthread a.cpp
这样就没有问题了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP