59

I am running Eclipse Helios and I have g++-4.6 installed. Hope I am not wrong that g++4.6 implements C++ 11 features. I have created a C++ project which uses the nullptr and auto keywords. The build gives the following errors:-

../{filename}.cpp:13:13: error: ‘nullptr’ was not declared in this scope

../{filename}.cpp:14:2: warning: ‘auto’ will change meaning in C++0x; please remove it [-Wc++0x-compat]

Actually it was building fine until yesterday. I am getting these from nowhere today. Please help me solve this problem.

N.N.
  • 8,336
  • 12
  • 54
  • 94
Higher-Kinded Type
  • 1,964
  • 5
  • 27
  • 44

7 Answers7

60

According to the GCC page for C++11:

To enable C++0x support, add the command-line parameter -std=c++0x to your g++ command line. Or, to enable GNU extensions in addition to C++0x extensions, add -std=gnu++0x to your g++ command line. GCC 4.7 and later support -std=c++11 and -std=gnu++11 as well.

Did you compile with -std=gnu++0x ?

Rob I
  • 5,627
  • 2
  • 21
  • 28
  • 3
    I tried adding -std=gnu++0x and -std=c++0x command line parameters but getting the same problem. I added it under Project Properties -> C/C++ Build -> Discovery Options -> Compiler Invocation Arguments. Is that the right place? – Higher-Kinded Type Apr 05 '12 at 17:59
  • Probably, but it sounds like the other answer is more grounded in reality. :) – Rob I Apr 05 '12 at 18:06
  • Hi Rob, I noticed one thing now. The IDE does not pick up the compiler argument -std=c++0x although I have added it under Project Properties -> C/C++ Build -> Discovery Options -> Compiler Invocation Arguments. So I tried the command line build 'g++ "../CPP.cpp" -std=gnu++0x -std=c++0x' in a terminal and it compiles fine; produces the binary, which runs successfully. So the IDE does not consider the compiler option specified. The makefile generated does not bear the compiler argument -std=c++0x. Not sure how to force the IDE to consider the compiler argument supplied! – Higher-Kinded Type Apr 05 '12 at 18:33
  • You only need one of the options, and I suggest the `-std=c++11`or -std=c++0x` depending on your compiler version. The `gnu++0x` enables non-standard extensions. – rubenvb Apr 05 '12 at 18:45
  • 2
    Finally found out what to do. Add the -std=c++0x compiler argument under Project Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous. It works now! But how to add this flag by default for all C++ projects? Anybody? Thanks Vivek Ragunathan – Higher-Kinded Type Apr 05 '12 at 18:58
22

Finally found out what to do. Added the -std=c++0x compiler argument under Project Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous. It works now!

But how to add this flag by default for all C++ projects? Anybody?

Higher-Kinded Type
  • 1,964
  • 5
  • 27
  • 44
6

You are using g++ 4.6 version you must invoke the flag -std=c++0x to compile

g++ -std=c++0x *.cpp -o output

The Beast
  • 1,629
  • 2
  • 29
  • 42
4

Is that an actual compiler error or a Code Analysis error? Some times the code analysis can be a bit sketchy and report non-valid errors.

To turn off code analysis for the project, right click on your project in the Project Explorer, click on Properties, then go to the C/C++ General tab, then Code Analysis. Then click on "Use Project Settings" and disable the ones that you do not wish for.

Also, are you sure you are compiling with the C++11 compiler?

josephthomas
  • 3,256
  • 15
  • 20
  • 2
    Upvoted to counter the downvote. This is almost certainly the problem. – Ed S. Apr 05 '12 at 17:51
  • Thank you. I have run into this issue way to many times, so I thought it was worth mentioning. – josephthomas Apr 05 '12 at 17:52
  • I don't see a code analysis item under C/C++ General tab in my Eclipse project properties. I am using g++4.6. Hope that implements C++11 features, specifically nullptr and auto. Thanks. – Higher-Kinded Type Apr 05 '12 at 18:02
  • Interesting, what version of eclipse do you have? To find out go to Help -> About Eclipse. I am on "Build id: 20120216-1857" and I do not have a problem with seeing it. – josephthomas Apr 05 '12 at 18:04
  • Snapshot of Help -> About Eclipse Eclipse Java EE IDE for Web Developers. Version: Helios Release Build id: 20100617-1415 Thanks – Higher-Kinded Type Apr 05 '12 at 18:06
  • I am not sure if even have code analysis at all with that build, so that may not be your error. Or it may just be in a different place. I did notice though that is not the version I have. I have the latest from http://www.eclipse.org/downloads/packages/eclipse-ide-cc-developers-includes-incubating-components/indigosr2 . Perhaps if you update you may fix your error. – josephthomas Apr 05 '12 at 18:08
  • OK I noticed one thing now. The IDE does not pick up the compiler argument -std=c++0x although I have added it under Project Properties -> C/C++ Build -> Discovery Options -> Compiler Invocation Arguments. So I tried the command line build 'g++ "../CPP.cpp" -std=gnu++0x -std=c++0x' in a terminal and it compiles fine; produces the binary, which runs successfully. So the IDE does not consider the compiler option specified. The makefile generated does not bear the compiler argument -std=c++0x. Not sure how to force the IDE to consider the compiler argument supplied! – Higher-Kinded Type Apr 05 '12 at 18:33
  • 2
    Finally found out what to do. Add the -std=c++0x compiler argument under Project Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous. It works now! But how to add this flag by default for all C++ projects? Anybody? Thanks Vivek Ragunathan – Higher-Kinded Type Apr 05 '12 at 19:00
  • @Vivek Ragunathan: Did you look into Window -> Preferences -> C/C++ -> Build -> Settings? – josephthomas Apr 05 '12 at 20:28
  • So, can you please explain if it's possible to introduce `nullptr` and other keywords to code analyzer or not? I would like to stay with code analyzer... – sorush-r May 03 '13 at 06:04
3

Go to Settings -> Compiler... And add flag to "Have g++ follow the coming C++0x ISO C++ language standard [std=c++0x]

Leon
  • 147
  • 2
  • 7
1

Trying with a different version of gcc worked for me - gcc 4.9 in my case.

JobJob
  • 3,822
  • 3
  • 33
  • 34
0

I add the ",-std=c++0x" after "-c -fmessage-length=0",under Project Properties -> C/C++ Build -> Settings -> GCC C++ Compiler -> Miscellaneous. Dont't forget to add the comma "," as the seperator.

qie
  • 21
  • 2