1

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?

shinzou
  • 5,850
  • 10
  • 60
  • 124
  • 2
    That's the primary sign of [UB](https://en.wikipedia.org/wiki/Undefined_behavior). I'd recommend double-check your code first. – Sourav Ghosh Mar 13 '15 at 11:44
  • 1
    don't focus on why the code is not running, but help him reproduce it on his local box. – Alexander Oh Mar 13 '15 at 11:49
  • 1
    This [question](http://stackoverflow.com/q/1936719/1708801) will tell you how to get the version programmatically but you need to figure out what is wrong with the code. It is unlikely to be a compiler bug in which case the version number does not really matter – Shafik Yaghmour Mar 13 '15 at 11:50
  • @Alex Can you elaborate please? – Sourav Ghosh Mar 13 '15 at 11:51
  • 2
    @SouravGhosh he says the online compiler segfaults. his home box runs the code fine. So apparently he wants to find out whats wrong with his code, by getting it segfault on his box. It's easiest to figure out undefined behaviour when so you see where it segfaults. – Alexander Oh Mar 13 '15 at 11:53
  • @Alex but that has _hardly_ anything to do with the compiler version which OP is asking for. See my first comment. – Sourav Ghosh Mar 13 '15 at 11:56
  • @SouravGhosh I have seen differences in gcc 4.7 and gcc 4.8 on arm, where UB has caused a fine built kernel or fails to boot. – Alexander Oh Mar 13 '15 at 11:58
  • Is it really UB if everything works fine on two different machines with different compilers? – shinzou Mar 13 '15 at 11:58
  • @kuhaku yes by definition undefined behavior is unpredictable and so on different machines you can get different behavior and that would be fully compliant with the standard. – Shafik Yaghmour Mar 13 '15 at 12:10

1 Answers1

6

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
Alexander Oh
  • 24,223
  • 14
  • 73
  • 76
  • I don't know how to make this code to print something. Also both codes in the two links produce many errors. – shinzou Mar 13 '15 at 11:59
  • I don't know how to use it... Can you tell me what to look up? Do I need to add some sort of macros to my code similar to what's in the CMake code? – shinzou Mar 14 '15 at 11:22