How do I use standard version flags (-std=...) with GCC?

Using standard version flags with GCC (GNU Compiler Collection) can help you specify which version of the C++ standard you want to use during compilation. This allows you to enforce compatibility with a specific standard, ensuring your code behaves consistently across different environments.

To use a standard version flag, you can include the -std= option followed by the desired C++ standard. Here are some common versions:

  • -std=c++98 for C++98
  • -std=c++11 for C++11
  • -std=c++14 for C++14
  • -std=c++17 for C++17
  • -std=c++20 for C++20
  • -std=c++2a (or -std=c++20) for features from C++20

For example, to compile a file named example.cpp using the C++14 standard, you would use the following command in your terminal:

g++ -std=c++14 example.cpp -o example

GCC C++ standard standard version flags g++ compilation C++98 C++11 C++14 C++17 C++20