2

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

4
  • 1
    Many compilers provide several pre-defined macros or directives that help with this. For gcc see: gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html. If you are only choosing between gcc and Visual C++ then checking for gcc may be adequate. If you're arbitrating between several, then you'll need to check for pre-defined macros in Visual C++ documentation, etc. Commented Jul 25, 2013 at 10:52
  • 1
    The MS Visual C++ predefined macros. Commented Jul 25, 2013 at 10:53
  • @Hossein Are you interested in the C++ standard that is supported by the compiler or are you interested in the version of the compiler itself? Commented Jul 25, 2013 at 11:13
  • @HETEPEPERFAN:Actually both of them. The reason i am asking this is primarily for finding about the C++11 compilance , and then i need to know version in case i am dealing with codes (macros?directives?) that are supported in some versions and not the other, so they both are important to me and i just don't know which answer to choose now! :-/ Commented Jul 25, 2013 at 11:28

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

__cplusplus doesn't tell us the version of the "compiler". It tells us the version of the "language".
But the question does suggest that OP is interested in the language version supported by a compiler version. Knowing only the compiler version leaves open the question of whether or not a snippet of C++11 can compiled or not.
@HighPerformanceMark: I think knowing the compiler version answers the question what features of C++11 can be compiled. Different versions of a compiler compilers different set of features even though the versions may define __cplusplus to be 201103L.
Although this is the best the standard offers, it's fairly useless in practice. In practice, you want to know which C++11 features your compiler supports and which it doesn't, because in practice all C++11 implementations are incomplete (unless GNU finished all the outstanding library todo items while I wasn't looking). So regardless of what they advertise, their C++11 mode in fact is not C++11 yet. So for example the Boost headers that deal with different compilers don't (and can't) do that just by looking at the value of __cplusplus.
@SteveJessop "this is the best the standard offers" - at least until C++20 and its feature testing capability is more wide-spread.
1

In gcc and clang, you could use __VERSION__ macro.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.