如何在 Eclipse CDT 中启用 C++11/C++0x 支持?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9131763/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:20:58  来源:igfitidea点击:

How to enable C++11/C++0x support in Eclipse CDT?

c++eclipsec++11eclipse-cdt

提问by Nick

Eclipse 3.7.1 CDT 1.4.1 GCC 4.6.2

Eclipse 3.7.1 CDT 1.4.1 GCC 4.6.2

This is an example of a piece of C++11 code:

这是一段 C++11 代码的示例:

auto text = std::unique_ptr<char[]>(new char[len]);

The Eclipse editor complains about:

Eclipse 编辑器抱怨:

Function 'unique_ptr' could not be resolved

The Makefile compilation works fine. How to make Eclipse stop complaining about these sort of errors?

Makefile 编译工作正常。如何让 Eclipse 停止抱怨此类错误?

回答by Carsten Greiner

I found thisarticle in the Eclipse forum, just followed those steps and it works for me. I am using Eclipse Indigo 20110615-0604 on Windows with a Cygwin setup.

我在 Eclipse 论坛中找到了这篇文章,只是按照这些步骤操作,它对我有用。我在带有 Cygwin 设置的 Windows 上使用 Eclipse Indigo 20110615-0604。

  • Make a new C++ project
  • Default options for everything
  • Once created, right-click the project and go to "Properties"
  • C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Miscellaneous -> Other Flags. Put -std=c++0x(or for newer compiler version -std=c++11at the end . ... instead of GCC C++ Compiler I have also Cygwin compiler
  • C/C++ General -> Paths and Symbols -> Symbols -> GNU C++. Click "Add..." and paste __GXX_EXPERIMENTAL_CXX0X__(ensure to append and prepend two underscores) into "Name" and leave "Value" blank.
  • Hit Apply, do whatever it asks you to do, then hit OK.
  • 创建一个新的 C++ 项目
  • 一切的默认选项
  • 创建后,右键单击项目并转到“属性”
  • C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Miscellaneous -> Other Flags。放置-std=c++0x(或用于较新的编译器版本-std=c++11在最后......而不是 GCC C++ 编译器我也有 Cygwin 编译器
  • C/C++ 通用 -> 路径和符号 -> 符号 -> GNU C++。单击“添加...”并粘贴__GXX_EXPERIMENTAL_CXX0X__(确保附加并添加两个下划线)到“名称”中,并将“值”留空。
  • 点击应用,做任何它要求你做的事情,然后点击确定。

There is a description of this in the Eclipse FAQ now as well: Eclipse FAQ/C++11 Features.

现在 Eclipse FAQ 中也有对此的描述:Eclipse FAQ/C++11 Features

Eclipse image setting

Eclipse 图像设置

回答by Trismegistos

Instruction For Eclipse CDT 4.4 Luna and 4.5 Mars

Eclipse CDT 4.4 Luna 和 4.5 Mars 说明

First, before creating project, configure Eclipse syntax parser:

首先,在创建项目之前,配置Eclipse语法解析器:

Window-> Preferences-> C/C++-> Build-> Settings-> Discovery-> CDT GCC Build-in Compiler Settings

Window-> Preferences-> C/C++-> Build-> Settings-> Discovery->CDT GCC Build-in Compiler Settings

in the text box entitled Command to get compiler specsappend -std=c++11

在标题为Command to get compiler specs附加的文本框中-std=c++11

Now you can create project, configuration depends on what kind of project you created:

现在您可以创建项目,配置取决于您创建的项目类型:

For project created as: File -> New -> Project -> C/C++ -> C++ Project

对于创建为:文件 -> 新建 -> 项目 -> C/C++ -> C++ 项目的项目

Right click on created project and open

右键单击创建的项目并打开

Properties-> C/C++ Build-> Settings-> Tool Settings-> GCC C++ Compiler-> Dialect

Properties-> C/C++ Build-> Settings-> Tool Settings-> GCC C++ Compiler->Dialect

Put -std=c++11into text box entitled other dialect flagsor select ISO C++11from the Language standarddrop down.

放入-std=c++11标题为的文本框other dialect flagsISO C++11Language standard下拉列表中选择。

For CMake project

对于 CMake 项目

Generate eclipse project files (inside your project)

生成 eclipse 项目文件(在你的项目中)

mkdir build
cd build
cmake -G"Eclipse CDT4 - Unix Makefiles" -D CMAKE_BUILD_TYPE=Debug ..

Then import generated directory to eclipse as standard eclipse project. Right click project and open

然后将生成的目录作为标准 eclipse 项目导入到 eclipse 中。右键单击项目并打开

Properties-> C/C++ General-> Preprocessor Include Paths, Marcos etc.-> Providers

Properties-> C/C++ General-> Preprocessor Include Paths, Marcos etc.->Providers

enable CDT GCC Build-in Compiler Settingsand move it higher than Contributed PathEntry Containers(This is important)

启用CDT GCC Build-in Compiler Settings并将其移动到高于Contributed PathEntry Containers(这很重要)

Last Common Step

最后一个通用步骤

recompile, regenerate Project->C/C++ Indexand restart Eclipse.

重新编译,重新生成Project->C/C++ Index并重新启动 Eclipse。

回答by Johan Lundberg

Update 2016:

2016 年更新:

As of gcc 6(changes), the default C++ dialect is C++14. That means that unless you explicitly need a newer or older dialect than than, you don't need to do anything with eclipse anymore.

gcc 6更改)开始,默认的 C++ 方言是 C++14。这意味着除非您明确需要比比方言更新或旧的方言,否则您不再需要对 eclipse 做任何事情。

For Luna and Mars

对于 Luna 和 Mars

This community wiki section incorporates the answer by Trismegistos;

这个社区维基部分包含了 Trismegistos 的答案;

1. Before creating project, configure Eclipse syntax parser:

1.在创建项目之前,配置Eclipse语法解析器:

Window -> Preferences -> C/C++ -> Build -> Settings -> Discovery -> CDT GCC Build-in Compiler Settings

Window -> Preferences -> C/C++ -> Build -> Settings -> Discovery -> CDT GCC Build-in Compiler Settings

in the text box entitled Command to get compiler specs append -std=c++142. Create project, configuration depends on what kind of project you created:

在标题为 Command to get compiler specs 的文本框中附加-std=c++142. 创建项目,配置取决于您创建的项目类型:

For project created as: File -> New -> Project -> C/C++ -> C++ Project

对于创建为:文件 -> 新建 -> 项目 -> C/C++ -> C++ 项目的项目

Right click on created project and open

右键单击创建的项目并打开

Properties -> C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Dialect

属性 -> C/C++ 构建 -> 设置 -> 工具设置 -> GCC C++ 编译器 -> 方言

Put -std=c++14into text box entitled other dialect flags or select ISO C++11 from the Language standard drop down.

放入-std=c++14标题为其他方言标志的文本框或从语言标准下拉列表中选择 ISO C++11。

There's now a new way to solve this without the GXX_EXPERIMENTAL hack.

现在有一种无需 GXX_EXPERIMENTAL hack 即可解决此问题的新方法。

For most recent versions: (Currently Juno and KeplerLuna):

对于最新版本:(目前为Juno 和 KeplerLuna):

Under newer versions of Juno the settings are located at Project properties -> C/C++ General -> Preprocessor Include Paths, Macros etc. -> tab Providers -> CDT GCC Builtin Compiler Settings ().

在较新版本的 Juno 下,设置位于Project properties -> C/C++ General -> Preprocessor Include Paths, Macros etc. -> tab Providers -> CDT GCC Builtin Compiler Settings ().

Older versions 2012/2013:

旧版本 2012/2013:

  1. Under C/C++ Build (at project settings), find the Preprocessor IncludePath and go to the Providers Tab. Deselect all except CDT GCC Builtin Compiler Settings. Then untag Share settings entries … . Add the option -std=c++11to the text box called Command to get compiler specs.

  2. Go to paths and symbols. Under Symbols, click restore defaults, and then apply.

  1. 在 C/C++ Build(在项目设置中)下,找到Preprocessor IncludePath 并转到Providers Tab。取消选择除 CDT GCC 内置编译器设置之外的所有选项。然后取消标记共享设置条目...。将选项 -std=c++11 添加到名为 Command 的文本框以获取编译器规范。

  2. 转到路径和符号。在符号下,单击恢复默认值,然后应用。



Notes:

笔记:

Eclipse is picky about hitting apply, you need to do it every time you leave a settings tab.

Eclipse 对点击应用很挑剔,每次离开设置选项卡时都需要这样做。

[Self-promotion]: I wrote my own more detailed instructions based on the above. http://scrupulousabstractions.tumblr.com/post/36441490955/eclipse-mingw-builds

【自我推销】:我在上面写了自己的更详细的说明。 http://scrupulousabstractions.tumblr.com/post/36441490955/eclipse-mingw-builds

Thanks to the user Nobodyat https://stackoverflow.com/a/13635080/1149664

由于用户Nobodyhttps://stackoverflow.com/a/13635080/1149664

回答by Andreas

For the latest (Juno) eclipse cdt the following worked for me, no need to declare __GXX_EXPERIMENTAL_CXX0X__on myself. This works for the the CDT indexer and as parameter for the compiler:

对于最新的(Juno)eclipse cdt,以下对我有用,无需__GXX_EXPERIMENTAL_CXX0X__对自己声明。这适用于 CDT 索引器并作为编译器的参数:

"your project name" -> right click -> properties:

“您的项目名称”-> 右键单击​​-> 属性:

C/C++ General -> Preprocessor Include Paths, Macros etc. -> switch to the tab named "Providers":

C/C++ General -> Preprocessor Include Paths, Macros etc. -> 切换到名为“Providers”的选项卡:

  • for "Configuration" select "Release" (and afterwards "debug")

  • switch off all providers and just select "CDT GCC Built-in Compiler Settings"

  • uncheck "Share setting entries between projects (global provider)"

  • in the "Command to get compiler specs:" add "-std=c++11" without the quotes (may work with quotes too)

  • hit apply and close the options

  • rebuild the index

  • 对于“配置”选择“发布”(然后是“调试”)

  • 关闭所有提供程序,然后选择“CDT GCC 内置编译器设置”

  • 取消选中“在项目(全球提供商)之间共享设置条目”

  • 在“获取编译器规范的命令:”中添加不带引号的“-std=c++11”(也可以使用引号)

  • 点击应用并关闭选项

  • 重建索引

Now all the c++11 related stuff should be resolved correctly by the indexer.

现在索引器应该正确解析所有与 c++11 相关的内容。

win7 x64, latest official eclipse with cdt mingw-w64 gcc 4.7.2from the mingwbuilds project on sourceforge

win7 x64,来自 sourceforge mingwbuilds 项目的带有 cdt mingw-w64 gcc 4.7.2 的最新官方 Eclipse

回答by Jerome

I had the same problem on my Eclipse Juno. These steps solved the problem :

我在 Eclipse Juno 上遇到了同样的问题。这些步骤解决了这个问题:

  • Go to Project -> Properties -> C/C++ General -> Path and Symbols -> Tab [Symbols].
  • Add the symbol : __cpluspluswith the value 201103L
  • Project -> Properties -> C/C++ General -> Path and Symbols -> Tab [Symbols]
  • 添加符号:__cplusplus,值为201103L

回答by user3348915

For Eclipse CDT Kepler what worked for me to get rid of std::threadunresolved symbol is:

对于 Eclipse CDT Kepler,对我来说摆脱std::thread未解析符号的方法是:

  1. Go to Preferences->C/C++->Build->Settings

  2. Select the Discovery tab

  3. Select CDT GCC Built-in Compiler Settings [Shared]

  4. Add the -std=c++11 to the "Command to get the compiler specs:" field such as:

  1. 转到首选项-> C/C++-> 构建-> 设置

  2. 选择发现选项卡

  3. 选择 CDT GCC 内置编译器设置 [共享]

  4. 将 -std=c++11 添加到“获取编译器规范的命令:”字段中,例如:

${COMMAND} -E -P -v -dD -std=c++11 ${INPUTS}

${COMMAND} -E -P -v -dD -std=c++11 ${INPUTS}

  1. Ok and Rebuild Index for the project.
  1. 确定并重建项目的索引。

Adding -std=c++11to project Properties/C/C++ Build->Settings->Tool Settings->GCC C++ Compiler->Miscellaneous->OtherFlags wasn't enough for Kepler, however it was enough for older versions such as Helios.

将 Flags添加-std=c++11到项目中Properties/C/C++ Build->Settings->Tool Settings->GCC C++ Compiler->Miscellaneous->Other对于 Kepler 来说还不够,但是对于 Helios 等旧版本来说已经足够了。

回答by ravwojdyla

I can't yet comment so am writing my own answer:

我还不能发表评论,所以我正在写我自己的答案:

It's related to __GXX_EXPERIMENTAL_CXX0X__and it's valid for Eclipse Juno and CDT 8.x.

它与__GXX_EXPERIMENTAL_CXX0X__Eclipse Juno 和 CDT 8.x相关并且对它有效。

Some parts of this answer are already covered in other answers but I want it to be coherent.

这个答案的某些部分已经包含在其他答案中,但我希望它是连贯的。

To make it possible to build using stdc++11, one have to add specific flag for compiler. You can do that via project properties. To modify project properties RMBandProject propertiesor ALT + ENTER. Then C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Miscellaneous -> Other Flags. Put -std=c++11 at the end of line, for GCC it will look something like: -c -fmessage-length=0 -std=c++11. By adding -stdc++11 flag compiler (GCC) will declare __GXX_EXPERIMENTAL_CXX0X__by itself.

为了能够使用 stdc++11 进行构建,必须为编译器添加特定的标志。您可以通过项目属性来做到这一点。修改项目属性RMBProject propertiesALT + ENTER。然后C/C++ Build -> Settings -> Tool Settings -> GCC C++ Compiler -> Miscellaneous -> Other Flags。把-std = C ++ 11日线的末端,对GCC它看起来是这样的:-c -fmessage-length=0 -std=c++11。通过添加 -stdc++11 标志编译器(GCC)将__GXX_EXPERIMENTAL_CXX0X__自行声明。

At this point you can build project using all the goodness of C++11.

此时,您可以使用 C++11 的所有优点构建项目。

The problem is that Eclipse has it's own parser to check for errors - that's why you're still getting all the nasty errors in Eclipse editor, while at the same time you can build and run project without any. There is a way to solve this problem by explicitly declaring __GXX_EXPERIMENTAL_CXX0X__flag for the project, one can do that (just like Carsten Greiner said): C/C++ General -> Paths and Symbols -> Symbols -> GNU C++. Click "Add..." and past __GXX_EXPERIMENTAL_CXX0X__(ensure to append and prepend two underscores) into "Name" and leave "Value" blank. And now is the extra part I wanted to cover in comment to the first answer, go to: C/C++ General -> Preprocessor Include Path Macros etc. -> Providers, and Select CDT Managed Build Setting Entriesthen click APPLY and go back to Entries tab, under GNU C++ there should be now CDT Managed Build Setting Entries check if inside there is defined __GXX_EXPERIMENTAL_CXX0X__if it is -> APPLY and rebuild index you should be fine at this point.

问题是 Eclipse 有它自己的解析器来检查错误——这就是为什么你仍然在 Eclipse 编辑器中遇到所有令人讨厌的错误,同时你可以构建和运行没有任何错误的项目。有一种方法可以通过__GXX_EXPERIMENTAL_CXX0X__为项目显式声明标志来解决这个问题,可以这样做(就像 Carsten Greiner 所说的那样):C/C++ General -> Paths and Symbols -> Symbols -> GNU C++。单击“添加...”并过去__GXX_EXPERIMENTAL_CXX0X__(确保附加并预先添加两个下划线)到“名称”并将“值”留空。现在是我想在第一个答案的评论中涵盖的额外部分,转到:C/C++ General -> Preprocessor Include Path Macros etc. -> Providers,然后选择CDT Managed Build Setting Entries然后单击 APPLY 并返回 Entries 选项卡,在 GNU C++ 下,现在应该有 CDT Managed Build Setting Entries 检查内部是否已定义__GXX_EXPERIMENTAL_CXX0X__-> APPLY 并重建索引此时您应该没问题。

回答by Ola Aronsson

I had several issues too (Ubuntu 13.04 64-bit, g++-4.8, eclipse Juno 3.8.1, CDT 6.0.0). A lot of things are mentioned above, sorry to repeat those, but additionally I had problems utilizing

我也有几个问题(Ubuntu 13.04 64 位,g++-4.8,eclipse Juno 3.8.1,CDT 6.0.0)。上面提到了很多东西,很抱歉重复这些,但另外我在使用时遇到了问题

std::thread

as part of c++11 (adding -pthread for the linker solves that issue). Anyway, finally these settings worked fine:

作为 c++11 的一部分(为链接器添加 -pthread 可以解决该问题)。无论如何,最后这些设置工作正常:

Project -> Properties -> C/C++ Build -> Settings -> Miscellaneous. Add the

项目 -> 属性 -> C/C++ 构建 -> 设置 -> 杂项。添加

-std=c++11

flag for the GCC and G++ compilers. Click Apply.

GCC 和 G++ 编译器的标志。单击应用。

For the linker, same window, Miscellaneous, Linker flags, added the

对于链接器,相同的窗口,杂项,链接器标志,添加了

-pthread

flag. Shared library settings, Shared object name, add the

旗帜。共享库设置,共享对象名称,添加

-Wl,--no-as-needed

flag too. Click Apply.

国旗也是。单击应用。

C/C++ General -> Paths and symbols -> Symbols TAB, GNU C++ selected, Add the

C/C++ General -> Paths and symbols -> Symbols TAB,选择GNU C++,添加

__GXX_EXPERIMENTAL_CXX0X__

(no value)

(没有价值)

flag. Click Apply.

旗帜。单击应用。

C/C++ General -> Preprocessor Include paths.. -> Providers tab : check

C/C++ General -> Preprocessor Include paths.. -> Providers 选项卡:检查

CDT GCC built-in Compiler Settings

CDT GCC 内置编译器设置

and for "Command to get compiler specs", add the

对于“获取编译器规范的命令”,添加

-std=c++11

flag. Uncheck Share. Click Apply.

旗帜。取消选中共享。单击应用。

CDT Managages Build Setting Entries, check this too. Uncheck the two others. Click Apply.

CDT 管理构建设置条目,也检查这个。取消选中另外两个。单击应用。

Going back to the Entries tab, GNU C++ CDT Managages Build Setting Entries, you should now see your added

返回条目选项卡,GNU C++ CDT 管理构建设置条目,您现在应该看到您添加的

__GXX_EXPERIMENTAL_CXX0X__

entry.

入口。

That's it. When coding, typing

就是这样。编码时,键入

std::

can now auto-complete the thread class for instance, builds should work fine and there should be no

例如,现在可以自动完成线程类,构建应该可以正常工作,并且应该没有

std::system_error'what(): Enable multithreading to use std::thread: Operation not permitted

at runtime.

在运行时。

回答by doody1986

I don't know if it is only me, the highest ranked solution doesn't work for me, my eclipse version is just normal eclipse platform installed by using sudo apt-get install eclipse in Ubuntu But I found a solution which adopts method together from both the highest ranked solution and the second, what I did to make it work is described as below (Note that the other steps like creating a C++ project etc. is ignored for simplicity)

我不知道是不是只有我,排名最高的解决方案对我不起作用,我的 eclipse 版本只是在 Ubuntu 中使用 sudo apt-get install eclipse 安装的普通 eclipse 平台但我找到了一个解决方案,它采用了方法从排名最高的解决方案和第二个解决方案中,我为使其工作所做的描述如下(请注意,为简单起见,忽略了其他步骤,例如创建 C++ 项目等)

Once you have created the C++ project

创建 C++ 项目后

(1) C/C++ General -> Paths and Symbols -> Symbols -> GNU C++. Click "Add..." and paste GXX_EXPERIMENTAL_CXX0X(ensure to append and prepend two underscores) into "Name" and leave "Value" blank.

(1) C/C++ General -> Paths and Symbols -> Symbols -> GNU C++。单击“添加...”并将GXX_EXPERIMENTAL_CXX0X(确保附加和前置两个下划线)粘贴到“名称”中,并将“值”留空。

(2) Under C/C++ Build (at project settings), find the Preprocessor Include Path and go to the Providers Tab. Deselect all except CDT GCC Builtin Compiler Settings. Then untag Share settings entries … . Add the option -std=c++11 to the text box called Command to get compiler specs

(2) 在 C/C++ Build 下(在项目设置中),找到 Preprocessor Include Path 并转到 Providers 选项卡。取消选择除 CDT GCC 内置编译器设置之外的所有选项。然后取消标记共享设置条目...。将选项 -std=c++11 添加到名为 Command 的文本框以获取编译器规范

After performed above 2 and 2 only steps, it works, the eclipse is able to resolve the unique_ptr, I don't know why this solution works, hope that it can help people.

执行完以上2和2步骤后,它起作用了,eclipse能够解析unique_ptr,我不知道这个解决方案为什么起作用,希望它可以帮助人们。

回答by Rubens Gomes

Eclipse C/C++ does not recognize the symbol std::unique_ptreven though you have included the C++11 memory header in your file.

std::unique_ptr即使您在文件中包含了 C++11 内存标头,Eclipse C/C++ 也无法识别该符号。

Assuming you are using the GNU C++ compiler, this is what I did to fix:

假设您使用的是 GNU C++ 编译器,这就是我要修复的内容:

Project -> Properties -> C/C++ General -> Preprocessor Include Paths -> GNU C++ -> CDT User Setting Entries

Project -> Properties -> C/C++ General -> Preprocessor Include Paths -> GNU C++ -> CDT User Setting Entries

  1. Click on the "Add..." button

  2. Select "Preprocessor Macro" from the dropdown menu

    Name: __cplusplus     Value:  201103L
    
  3. Hit Apply, and then OK to go back to your project

  4. Then rebuild you C++ index: Projects -> C/C++ Index -> Rebuild

  1. 单击“添加...”按钮

  2. 从下拉菜单中选择“预处理器宏”

    Name: __cplusplus     Value:  201103L
    
  3. 点击 Apply,然后 OK 回到你的项目

  4. 然后重建你的 C++ 索引:项目 -> C/C++ 索引 -> 重建