关于在ubuntu下eclipse中c++11支持和编译运行c++11报错的解决方法

最近准备升级项目中的c++线程部分代码用c++11简单重构一下,结果测试demo直接就报错~

百度和谷歌了一部分还是没有解决这个问题~

最后经过研究eclipse的自动编译~终于搞定了~

首先按照网上说的~ http://hkllzh.iteye.com/blog/1620352 配置eclipse的编译参数,我的平台是ubuntu14 64位 3.11.0-26-generic

编译通过了,运行报错:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)

后来在谷歌找到 : http://stackoverflow.com/questions/21129735/g-4-8-1-c-threads-stdsystem-error-operation-not-permitted

需要添加

-Wl,--no-as-needed -pthread
C/C++ Build --> Settings --> Tool Settings --> GCC C++ Compiler --> Miscellanous中添加这个编译参数

然后发现还是不行-_-#

后来根据eclipse输出

make all
Building file: ../src/TestC11.cpp
Invoking: GCC C++ Compiler
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -pthread -std=c++0x  -Wl,--no-as-needed -MMD -MP -MF"src/TestC11.d" -MT"src/TestC11.d" -o "src/TestC11.o" "../src/TestC11.cpp"
Finished building: ../src/TestC11.cpp
 
Building target: TestC11
Invoking: GCC C++ Linker
g++  -o "TestC11"  ./src/TestC11.o   
Finished building target: TestC11

发现了问题~eclipse是先编译成静态库然后生成可执行文件,那么要在链接阶段添加这个参数而不是编译阶段~~~

那么在C/C++ Build --> Settings --> Tool Settings --> GCC C++ Linker --> Miscellanous 添加,在原先编译参数的地方删除

-Wl,--no-as-needed -pthread

再次clean - build - run 执行成功~

=================================================   别高兴的太早  =================================================

回到eclipse一看~诶~编译运行时通过了~但是代码中还是报错~thread类找不到,并且自动提示也没有,那还敲什么代码~~~

进入头文件<thread>查看发现thread声明有宏定义,并且

#if __cplusplus < 201103L
# include <bits/c++0x_warning.h>
#else
...
#endif
并且

__cplusplus 199711L

恩~上面添加的c++11支持并没有在代码检查中生效~

最后查到设置在

Workspace settings

Window-> Preference -> Build -> Settings -> Discovery -> CDT GCC Built-in Compiler Settings(shared) 中的
(command to get compiler specs) 改为 ${COMMAND} -E -P -v -dD ${INPUTS} -std=c++11
最后还要重新新建工程~刷新或者改工程中的
CDT GCC Built-in Compiler Settings(shared)
参数也是不行的~必须新建一个~



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值