先看下一个出错的场景,我们在Visual studio 2010项目中添加如下头文件不会出错,但是在eclipse就会出错。
/*
* Test.h
*/
#ifndef TEST_H_
#define TEST_H_
class Test {
public:
Test();
~Test();
private:
std::shared_ptr<Test> test;
};
#endif /* TEST_H_ */
出错的原因在编译器不认识shared_ptr.
shared_ptr是一种智能指针,被C++标准委员会加入到11标准中,它因此成为了C++0x的一部分。
有了这个背景知识,可能会在错误提示找到一些头绪,设置Eclipse使用C++0x标准
具体步骤
1. 右键项目 -> properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous
添加 ”-std=c++0x" ,不要引号 ,然后各种apply 和OK
2. 右键项目 -> properties -> C/C++ General -> Preprocessor Include Paths,Macros, etc. -> Providers
勾选
CDT GCC Built in Compiler Settings MinGW ,因为我用的是MinGW
取消 下面的Use gloabal provider shared between projects
同样在下面的Command to get Compiler spes 的值的后面加上“std=c++0x” 不要引号
然后apply and OK
3. 返回项目workspace ,选择Project菜单 -> C/C++ index -> Re-resolve unresolved includes
现在你的Eclipse支持c++11标准了,当然错误也没了。
参考链接
http://stackoverflow.com/questions/17457069/enabling-c11-in-eclipse-juno-kepler-cdt?lq=1