is it possible to get the version of compiler in code? for example using some compiler directives?
I am trying to find about the version of a compiler and then lets say if the version of Gcc or Visual C++ is C++11 compliant then compile this bit of code and if not it compile thats snippet instead
3 Answers
You can use __cplusplus macro to check if compiler supports C++11 so that it will work even on compilers you don't know about.
#if __cplusplus >= 201103L //C++ 11 code here #endif 16.8 Predefined macro names
1 The following macro names shall be defined by the __cplusplus The name __cplusplus is defined to the value 201103L when compiling a C++ translation unit.
157) It is intended that future versions of this standard will replace the value of this macro with a greater value. Non-conforming compilers should use a value with at most five decimal digits.
5 Comments
__cplusplus doesn't tell us the version of the "compiler". It tells us the version of the "language".__cplusplus to be 201103L.__cplusplus.If you want to know what compiler you're using, they have their own predefined macros for that, linked in other comments. But you're indicating that you are doing this in order to discover the presence of C++11 support. In that case, the correct code is
#if __cplusplus <= 199711L //No C++11 support #else //Congratulations, C++11 support! #endif According to the standard, compilers are required to set that variable, and it indicates the version. See it on Bjarne's page
gccsee: gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html. If you are only choosing betweengccandVisual C++then checking forgccmay be adequate. If you're arbitrating between several, then you'll need to check for pre-defined macros inVisual C++documentation, etc.