Eclipse 3.7.1 CDT 1.4.1 GCC 4.6.2

下面是c++ 11代码的一个例子:

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

Eclipse编辑器抱怨:

Function 'unique_ptr' could not be resolved

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


当前回答

右键单击项目,选择“属性” C/ c++ Build -> Settings -> Tool Settings -> GCC c++ Compiler -> Miscellaneous -> Other Flags。将-lm放在其他标志文本框的末尾,然后确定。

其他回答

我还不能评论,所以我写了我自己的答案:

它与__GXX_EXPERIMENTAL_CXX0X__相关,对Eclipse Juno和CDT 8.x有效。

这个答案的一些部分已经在其他答案中涵盖了,但我希望它是连贯的。

为了能够使用stdc++11构建,必须为编译器添加特定的标志。您可以通过项目属性来实现这一点。修改项目属性RMB和项目属性或ALT + ENTER。然后C/ c++构建->设置->工具设置-> GCC c++编译器-> Miscellaneous ->其他标志。将-std=c++11放在行末,对于GCC来说,它看起来像这样:-c -fmessage-length=0 -std=c++11。通过添加-stdc++11标志,编译器(GCC)将自己声明__GXX_EXPERIMENTAL_CXX0X__。

此时,您可以使用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 Entries then 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.

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

std::thread

作为c++11的一部分(为链接器添加-pthread可以解决这个问题)。不管怎样,最终这些设置工作正常:

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

-std=c++11

GCC和g++编译器的标志。单击Apply。

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

-pthread

国旗。共享库设置、共享对象名称、添加

-Wl,--no-as-needed

国旗。单击Apply。

C/ c++通用->路径和符号->符号选项卡,GNU c++选中,添加

__GXX_EXPERIMENTAL_CXX0X__

(没有价值)

国旗。单击Apply。

预处理器包含路径..—>提供商页签:检查

CDT GCC内置编译器设置

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

-std=c++11

国旗。取消共享。单击Apply。

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

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

__GXX_EXPERIMENTAL_CXX0X__

条目。

就是这样。编码时,打字

std::

现在可以自动完成线程类,例如,构建应该工作良好,应该没有

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

在运行时。

右键单击项目,选择“属性” C/ c++ Build -> Settings -> Tool Settings -> GCC c++ Compiler -> Miscellaneous -> Other Flags。将-lm放在其他标志文本框的末尾,然后确定。

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

创建了c++项目之后

(1) C/ c++通用->路径和符号->符号-> GNU c++。点击“Add…”,将GXX_EXPERIMENTAL_CXX0X(确保在“Name”中添加和前置两个下划线)粘贴到“Name”中,“Value”为空。

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

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

我在Eclipse论坛上找到了这篇文章,只需要按照这些步骤,它就适合我了。我在Windows上使用Cygwin设置的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++11 at 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.

现在在Eclipse FAQ/ c++ 11 Features中也有关于这方面的描述。

Eclipse设置