您的位置:首页 > 编程语言 > C语言/C++

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

2014-12-10 10:44 495 查看
最近准备升级项目中的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)
参数也是不行的~必须新建一个~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++11 c++0x thread eclipse