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

来源:互联网 发布:js调用接口获取数据 编辑:程序博客网 时间:2024/05/22 00:42

最近准备升级项目中的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 permittedAborted (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输出

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. make all  
  2. Building file: ../src/TestC11.cpp  
  3. Invoking: GCC C++ Compiler  
  4. 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"  
  5. Finished building: ../src/TestC11.cpp  
  6.    
  7. Building target: TestC11  
  8. Invoking: GCC C++ Linker  
  9. g++  -o "TestC11"  ./src/TestC11.o     
  10. 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声明有宏定义,并且

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #if __cplusplus < 201103L  
  2. # include <bits/c++0x_warning.h>  
  3. #else  
  4. ...  
  5. #endif  
并且

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. __cplusplus 199711L  

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

最后查到设置在

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Workspace settings  
  2.   
  3. Window-> Preference -> Build -> Settings -> Discovery -> CDT GCC Built-in Compiler Settings(shared) 中的  
  4. (command to get compiler specs) 改为 ${COMMAND} -E -P -v -dD ${INPUTS} -std=c++11  
最后还要重新新建工程~刷新或者改工程中的
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. CDT GCC Built-in Compiler Settings(shared)  
参数也是不行的~必须新建一个~
0 0