How to enable C++11 in Eclipse Juno/Kepler/Luna CDT?

user327301 picture user327301 · Jul 3, 2013 · Viewed 87.7k times · Source

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.

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

error: range-based-for loops are not allowed in C++98 mode

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.

Different comments have suggested restarting eclipse and cleaning and rebuilding. That hasn't made a difference.

Answer

MrEricSir picture MrEricSir · Jul 6, 2013

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.)

Setting up the compiler is fairly straightforward:

  1. Right click your project and click Properties
  2. Under C/C++ Build click Settings
  3. Under GCC C++ Compiler, click Miscellaneous
  4. In the Other Flags box, append "-std=c++11" to the list of tokens.
  5. Click Apply and OK

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:

  1. Right click your project and click Properties
  2. Under C/C++ General click "Preprocessor Include Paths, Macros"
  3. Select the Providers tab
  4. There should be an item in the list that says something like "GCC Built in Compiler Settings". Select this entry.
  5. Uncheck the "Use global provider..." option
  6. Under the list there's an box that says "Command to get compiler specs." Append "-std=c++0x" to this.
  7. Move the "GCC Built in Compiler Settings" provider at the top of the list using the 'Move Up' button on the right.
  8. Click Apply and then OK.
  9. Back in your Eclipse workspace, select the Project Menu, C/C++ Index, and click "Re-resolve unresolved includes."