20

I am currently typing all of my code in gedit and using the terminal to compile my programs. To do so, I am typing:

$ g++ main.cpp -o main
$ ./main

And this is working. However, it is not using C++11. How can I check my version of C++ and also compile via terminal using C++11?

52

This you can do by using the -std=c++11 flag. Here's an example:

g++ -std=c++11 -Wall -Wextra -Werror main.cpp -o main

This mode can be selected with the -std=c++11 command-line flag, or -std=gnu++11 to enable GNU extensions as well.(source)

See the explanation of the other flags below. I deeply believe that using at least those error flags will make your life easier in the long run. Once you have better knowledge of what your script does, you can omit warnings if needed to achieve a result but it should not be the standard. Hope this helps you. Here's a good place to start reading.

  • -Wall — enables all major warnings.
  • -Wextra — enables other important warnings.
  • -Werror — make all warnings into errors, causing compilations to fail if any warnings are reported.

Sources:

  1. GCC: Option Summary
  2. GCC: Options to Request or Suppress Warnings
  • 5
    Thanks! Do you mind explaining what purpose "-Wall" and "-Werror" serve in that command please? – user529161 May 16 '16 at 16:44
  • 1
    Added it to my answer. – Videonauth May 16 '16 at 16:51
  • 7
    @dhaneku.b: Why not read the documentation? That's what it's there for. – Lightness Races in Orbit May 16 '16 at 23:27
  • 9
    @LightnessRacesinOrbit Because the documentation isn't necessarily going to explain why Videonauth thought it was important to add them, despite the fact that they weren't in the original example commandline, or necessarily explain what sort of important interaction "-Wall" and "-Werror" might have with "-std=c++11". (Yes, the answer to the latter in this is "nothing special", but the documentation isn't going to say that, and there's a bunch of strange corner cases that aren't mentioned in the documentation. "Absence of proof isn't proof of absence", and all that.) – R.M. May 17 '16 at 0:14
  • 2
    This mode can be selected with the -std=c++11 command-line flag, or -std=gnu++11 to enable GNU extensions as well.(source) – Videonauth May 17 '16 at 12:58

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy