How do I use C++11 with g++?

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?


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