Get Clang Compiler Version Used to Compile C++ Code

Get Clang Compiler Version Used to Compile C++ Code

When working with C++ projects, knowing the exact version of the compiler used to build the code can be crucial for debugging, compatibility checks, or simply documenting the build environment. This tutorial demonstrates how to get Clang compiler version used to compile C++ code.

Clang exposes three macros __clang_major__, __clang_minor__ and __clang_patchlevel__ that define the major version, minor version, and patch level of the compiler, respectively.

The code below prints the compiler version in the format: major_version.minor_version.patch_level.

#include <iostream>

int main()
{
    std::cout << __clang_major__ << "."
              << __clang_minor__ << "."
              << __clang_patchlevel__ << std::endl;

    return 0;
}

An example of the output could be:

18.1.3

Leave a Comment

Cancel reply

Your email address will not be published.