Get gcc or g++ Compiler Version Used to Compile C++ Code

Get gcc or g++ Compiler Version Used to Compile C++ Code

Knowing the version of the gcc or g++ compiler used to compile C++ code can be helpful in various scenarios, such as identifying potential compatibility issues, or optimizing code to take advantage of new features in a particular version. This tutorial shows how to get the version of the gcc or g++ compiler used to compile C++ code.

The macros __GNUC__, __GNUC_MINOR__, and __GNUC_PATCHLEVEL__ are defined by all GNU compilers that use the C preprocessor, including C, C++, Objective-C, and Fortran. These macros define the major version, minor version, and patch level of the compiler, respectively.

The following code will output the compiler version in the format of major_version.minor_version.patch_level.

Note that these macros are only defined when compiling with the GNU compiler that use the C preprocessor (e.g. gcc, g++, gfortran), and may not be defined when compiling with other compilers.

#include <iostream>

int main()
{
    std::cout << __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__ << std::endl;

    return 0;
}

The output might look like this:

11.3.0

Leave a Comment

Cancel reply

Your email address will not be published.