最近准备升级项目中的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)
需要添加
-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)