When working with C or C++ projects, specifying the language standard during compilation is a good practice that helps ensure consistency, portability, and future-proofing of the code. If we don't explicitly tell the compiler which standard to use, it will default to its built-in preference, which can vary depending on the version of the compiler. This can lead to subtle bugs, compiler warnings, or incompatibilities when building the code on a different machine or toolchain. This tutorial explains how to compile with specific code standard using gcc or g++ compiler.
C Language
To compile a C file using a specific language standard, use the --std=
option with gcc. For example, to compile using the C17 standard:
gcc --std=c17 main.c -o test
To see all the -std=
options supported by the installed version of gcc, run:
gcc -v --help 2> /dev/null | grep '\-std='
C++ Language
To compile a C++ file with a specific standard, use the --std=
option with g++. For example, to compile using the C++17 standard:
g++ --std=c++17 main.cpp -o test
To check which -std=
options are supported by the version of g++, use the following command:
g++ -v --help 2> /dev/null | grep '\-std='
Leave a Comment
Cancel reply