shinzou
shinzou

Reputation: 6192

Is there a command that tells the compiler to print its version?

I need to upload my assignments to an online compiler, I was told it's GCC but I'm getting segfault on the online compiler but not when compiling with VS or on linux's GCC.

Is there a way to make compiler print what compiler is it and its version?

Upvotes: 1

Views: 2967

Answers (1)

Alexander Oh
Alexander Oh

Reputation: 25621

usually there isn't a single command.

you can try and check compiler defined macros.

cmake does this, it has a wide array of checks to detect compiler versions.

It compiles code and prints a "vendor string" based on preprocessor symbols.

here is for instance the code for gcc: https://github.com/Kitware/CMake/blob/master/Modules/Compiler/GNU-DetermineCompiler.cmake

since clang is drop in replacement for gcc you might also want to check the macros used here:

https://github.com/Kitware/CMake/blob/master/Modules/Compiler/Clang-C-FeatureTests.cmake

Edit:

So a running C example would do the following:

#include <stdio.h>

int main(int argc, char **argv) {
#ifdef __clang_major__
    printf ("clang detected version %d.%d\n", __clang_major__, __clang_minor__);
#endif

#ifdef __GNUC__
    // note that clang 3.7 declares itself as a gcc 4.2"
    printf ("gcc detected version %d.%d\n", __GNUC__, __GNUC_MINOR__);
#endif
}

output for clang:

$ clang main.cc
$ ./a.out 
clang detected version 3.7
gcc detected version 4.2

output for gcc:

$ gcc main.cc
$ ./a.out 
gcc detected version 4.8

Upvotes: 6

Related Questions