如何在 Eclipse Juno/Kepler/Luna CDT 中启用 C++11?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17457069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable C++11 in Eclipse Juno/Kepler/Luna CDT?
提问by user327301
EDIT: It turns out this really isn't specific to Eclipse Kepler. I had to use the same process for Eclipse Juno. The problem was that there seem to be missing steps in other posts answering this same question.
编辑:事实证明这真的不是 Eclipse Kepler 特有的。我必须对 Eclipse Juno 使用相同的过程。问题是在回答同一问题的其他帖子中似乎缺少步骤。
I'm using Eclipse Kepler for C++ and I'm trying to use C++11 and getting errors. When I compile I get the error
我正在将 Eclipse Kepler 用于 C++,而我正在尝试使用 C++11 并出现错误。当我编译时出现错误
error: range-based-for loops are not allowed in C++98 mode
错误:C++98 模式下不允许基于范围的 for 循环
I've followed the instructions from the post
我已按照帖子中的说明进行操作
Eclipse CDT C++11/C++0x support
and the solution given for Eclipse Juno isn't working.
为 Eclipse Juno 提供的解决方案不起作用。
Different comments have suggested restarting eclipse and cleaning and rebuilding. That hasn't made a difference.
不同的评论建议重新启动eclipse并清理和重建。这并没有什么不同。
回答by MrEricSir
There's two things you have to do, first you need to setup your compiler, then you need to setup CDT's language processor. Since you didn't mention which compiler you're using, I'll assume it's GCC but the steps will be similar for other compilers. (Note that you need a compiler that supports C++11, of course.)
您必须做两件事,首先需要设置编译器,然后需要设置 CDT 的语言处理器。由于您没有提到您使用的是哪个编译器,我假设它是 GCC,但其他编译器的步骤类似。(请注意,当然,您需要一个支持 C++11 的编译器。)
Setting up the compiler is fairly straightforward:
设置编译器相当简单:
- Right click your project and click Properties
- Under C/C++ Build click Settings
- Under GCC C++ Compiler, click Miscellaneous
- In the Other Flags box, append "-std=c++11" to the list of tokens.
- Click Apply and OK
- 右键单击您的项目,然后单击属性
- 在 C/C++ 构建下单击设置
- 在 GCC C++ Compiler 下,单击 Miscellaneous
- 在“其他标志”框中,将“-std=c++11”附加到标记列表中。
- 单击应用并确定
At this point you should be able to rebuild your project and get it to run. But CDT still may show errors for C++11 includes. Here's how you can resolve that:
此时,您应该能够重建您的项目并使其运行。但是 CDT 仍然可能会显示 C++11 包含的错误。以下是解决此问题的方法:
- Right click your project and click Properties
- Under C/C++ General click "Preprocessor Include Paths, Macros"
- Select the Providers tab
- There should be an item in the list that says something like "GCC Built in Compiler Settings". Select this entry.
- Uncheck the "Use global provider..." option
- Under the list there's an box that says "Command to get compiler specs." Append "-std=c++0x" to this.
- Move the "GCC Built in Compiler Settings" provider at the top of the list using the 'Move Up' button on the right.
- Click Apply and then OK.
- Back in your Eclipse workspace, select the Project Menu, C/C++ Index, and click "Re-resolve unresolved includes."
- 右键单击您的项目,然后单击属性
- 在 C/C++ 常规下单击“预处理器包括路径、宏”
- 选择提供者选项卡
- 列表中应该有一个项目,上面写着“GCC 内置编译器设置”之类的内容。选择此条目。
- 取消选中“使用全局提供程序...”选项
- 在列表下方有一个框,上面写着“获取编译器规范的命令”。将“-std=c++0x”附加到此。
- 使用右侧的“向上移动”按钮移动列表顶部的“GCC 内置编译器设置”提供程序。
- 单击应用,然后单击确定。
- 返回 Eclipse 工作区,选择 Project Menu、C/C++ Index,然后单击“Re-resolve unresolved includes”。
回答by MeJ
C++11 support in Eclipse Kepler Service Release 1 (Build id: 20130919-0819)
Eclipse Kepler Service Release 1 中的 C++11 支持(构建 ID:20130919-0819)
In the latest release Eclipse Kepler SR1 you only have to add -std=c++11
在最新版本的 Eclipse Kepler SR1 中,您只需添加-std=c++11
- Right click on your project and click Properties
- Navigate to C/C++ Generaland Preprocessor Include Paths, Macros etc.
- Select the Providerstab
- Add -std=c++11to Command to get compiler specs:
- Apply changes, the Index should be generated automatically.
- 右键单击您的项目,然后单击属性
- 导航到C/C++ General和Preprocessor Include Paths、Macros 等。
- 选择提供者选项卡
- 将-std=c++11添加到命令以获取编译器规范:
- 应用更改,索引应自动生成。
The "Command to get compiler specs:"-line should look like:
在“命令来获得编译器specs:”直插式应该是这样的:
${COMMAND} -E -P -v -dD "${INPUTS}" -std=c++11
回答by Tobu
As of CDT 8.3 (available as a Kepler update), there is a new dialect option in build settings:
从 CDT 8.3(可作为 Kepler 更新)开始,构建设置中有一个新的方言选项:
回答by Karrq
I use Eclipse Kepler and to fix it i did this:
我使用 Eclipse Kepler 并修复它,我这样做了:
1 - Right-Click the Project >> Proprietes >> C/C++ Build :: Settings
1 - 右键单击项目 >> 属性 >> C/C++ Build :: Settings
2 - Went to GCC G++ Compiler >> Miscellaneous >> Other Flags
2 - 转到 GCC G++ 编译器 >> 杂项 >> 其他标志
i then added -std=c++11
hit apply and ok
That fixed my problem!
那解决了我的问题!
I hope it helped!
我希望它有帮助!
回答by David Karla
Running eclipse indigo and cdt 8.0.2 here. I followed all the guides, but it was still necessary to set the preprocessor define by hand to find things like std::unique_ptr. The include file "memory" checks this. Set __cplusplus to 201103L on the "Symbols" tab under "Paths and Symbols" in "C++ General" in project properties.
在此处运行 eclipse indigo 和 cdt 8.0.2。我遵循了所有指南,但仍然需要手动设置预处理器定义以查找诸如 std::unique_ptr 之类的东西。包含文件“内存”对此进行了检查。在项目属性中“C++ 常规”中“路径和符号”下的“符号”选项卡上将 __cplusplus 设置为 201103L。
回答by mluthra
Adding dialect flag "-std=c++11" to Project properties-> C/C++ Build -> Settings -> Tool settings (GCC C++ Compiler -> Dialect) solved my problem in Eclipse Kepler, apart from the settings stated above.
除了上述设置之外,将方言标志“-std=c++11”添加到项目属性-> C/C++ 构建-> 设置-> 工具设置(GCC C++ 编译器-> 方言)解决了我在 Eclipse Kepler 中的问题。
回答by Miles F. Bintz II
I'm using Eclipse Luna and there is no "Tool Settings" under C/C++ Build -> Settings. I DIDadd -std=c++11 to the compiler specs command line under "C/C++ General -> Preprocessor Includes -> Providers" as suggested. But that still did not satisfy my indexer's ability to resolve emplace().
我正在使用 Eclipse Luna,并且 C/C++ Build -> Settings 下没有“工具设置”。我确实按照建议将 -std=c++11 添加到编译器规范命令行下的“C/C++ General -> Preprocessor Includes -> Providers”。但这仍然不能满足我的索引器解析 emplace() 的能力。
So I looked in the header file for and found that the emplace functions are dependent on __cplusplus >= 201103L. So I just added the preprocessor symbol "__cplusplus" and gave it the value 201103.
于是查看头文件,发现emplace函数依赖于__cplusplus >= 201103L。所以我只是添加了预处理器符号“__cplusplus”并赋予它值 201103。
Kinda hackish but the indexer is happy. (And makefiles define my build flags, not the eclipse project)
有点 hackish,但索引器很高兴。(并且生成文件定义了我的构建标志,而不是 eclipse 项目)
回答by StndFish
The solution provided by MrEricSir only works after having the latest Eclipse Oxygen CDT installed. It doesn't work for me with Mars CDT.
MrEricSir 提供的解决方案只有在安装了最新的 Eclipse Oxygen CDT 后才有效。使用 Mars CDT 时它对我不起作用。