7

So I'm following the tutorials on this page: http://www.cplusplus.com/doc/tutorial/control/ But I'm having trouble doing a range/based for loop. I found this page: GNU GCC compiler updatingThe answer there says I should open "Project" and "Properties". But when I try that, the "Properties" option is grayed out with no explanation: http://imageshack.com/a/img571/4371/xd1x.png So.. how can I activate range/based for loops?

Community
  • 1
  • 1
Sindre
  • 337
  • 2
  • 3
  • 8
  • 3
    The PNG link does not work. (This is why it's suggested to embed error messages in the question, rather than linking to external sites) – M.M Jan 28 '16 at 03:36

9 Answers9

20

Pass -std=c++11 flag to the compiler. Certainly GCC should be fresh enough (>=4.7) to support all these modern standards. For CodeBlocks 13.12: Settings -> Compiler -> Tab "Compiler Flags" -> Option "Have g++ follow the C++11 ISO C++ [-std=c++11]"

user3159253
  • 16,836
  • 3
  • 30
  • 56
  • I'm going through a beginner's tutorial, which means I'm a complete noob here. What does it mean to pass a flag to the compiler? – Sindre Apr 04 '14 at 09:54
  • Well, specify that string among other compiler options in the command line, like this: `g++ -std=c++11 -o test_executable test_source.cpp` – user3159253 Apr 04 '14 at 10:01
  • 1
    You're overestimating my knowledge, I have no idea where to write that – Sindre Apr 04 '14 at 10:12
  • How do you compile your code? In an IDE? Try to locate a place in the IDE menu (Settings?) where compiler invocation is configured. – user3159253 Apr 04 '14 at 10:42
  • Indeed, I use Code Blocks! Sorry, forgot to mention that. I posted a picture in the first post showing I can't access the properties for some reason – Sindre Apr 04 '14 at 10:44
  • GCC 4.7 supports some of C++11 but by no means all of it. Regex support is only available from 4.9 and many other features were introduced in 4.8. – Lightness Races in Orbit Apr 04 '14 at 11:22
  • But the range based for loop is in since 4.6 IIRC – gexicide Apr 04 '14 at 11:22
6

The above given solution of using -std=c++11 didn't work for me.

This is the target and version detail of my compiler.
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) Target: x86_64-linux-gnu

When I tried, this is what happened.
$ g++ -std=c++11 program.cpp cc1plus: error: unrecognized command line option ‘-std=c++11’

This solved the problem for me.
$ g++ -std=c++0x program.cpp

mythicalcoder
  • 3,143
  • 1
  • 32
  • 42
6

In Dev-Cpp 5.11 Simply you can click on Tools>Compiler Options>Settings>Code Generation>(and in last option)Language standard(-std) select from dropdown (ISO C++ 11).

enter image description here

BILAL ASLAM
  • 91
  • 1
  • 2
3

If you faced this error in Code::Blocks, this might help you -

Click on Settings -> Compiler -> Compiler Settings -> Compiler Flags

Under the "General" section, check [✓] the box beside : Have g++ follow the C++11 ISO C++ language standard [-std=c++11]

Check image for reference

Julian
  • 33,915
  • 22
  • 119
  • 174
2

Both of these:

g++ -std=c++11 -o test_executable test_source.cpp
g++ -std=c++0x -o program program.cpp

worked for me.

Only thing to do after compiling is to execute the test_executable (in the first case) as ./test_executables or program (in the second case) as ./program.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Akash Chavan
  • 77
  • 2
  • 9
1

If you are using QT5.5, you can achieve it by adding following lines in your .pro file.

CONFIG += c++11

mythicalcoder
  • 3,143
  • 1
  • 32
  • 42
subrat2014
  • 13
  • 2
0

Using the above solution g++ -std=c++0x program.cpp works. However, the command needs to be modified slightly in order to run the program with the common command: ./program

I used g++ -std=c++0x -o program program.cpp and all worked perfectly.

mythicalcoder
  • 3,143
  • 1
  • 32
  • 42
CTguest
  • 1
  • 1
0

The best solution is doing this, in Dev C++:

  1. Go to the "Tools" Option
  2. Select "Compiler Options"
  3. Click "Settings"
  4. Click "Code Generation"
  5. Go to "Choose Language" and select "ISO C++11"

Then your problem is resolved.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

in first time, if you have Dev-C++ C:\Program Files (x86)\Dev-Cpp\MinGW64\bin, you must add this route to the path then use the next command in cmd.

g++ -std=c++11 -o outprogram code_source.cpp

this command in your project directory.