These macros are defined by all GNU compilers that use the C preprocessor: C, C++, Objective-C and Fortran. Their values are the major version, minor version, and patch level of the compiler, as integer constants. For example, GCC 3.2.1 will define
to 2, and
to 1. These macros are also defined if you invoke the preprocessor directly.
is new to GCC 3.0; it is also present in the widely-used development snapshots leading up to 3.0 (which identify themselves as GCC 2.96 or 2.97, depending on which snapshot you have).
If all you need to know is whether or not your program is being compiled by GCC, or a non-GCC compiler that claims to accept the GNU C dialects, you can simply test
If you need to write code which depends on a specific version, you must be more careful. Each time the minor version is increased, the patch level is reset to zero; each time the major version is increased (which happens rarely), the minor version and patch level are reset. If you wish to use the predefined macros directly in the conditional, you will need to write it like this:
Another approach is to use the predefined macros to calculate a single number, then compare that against a threshold:
Many people find this form easier to understand.
If you found this post or this website helpful and would like to support our work, please consider making a donation. Thank you!Help Us
Articles in the same category
- Undefined Symbol ___gxx_personality_v0
- gcc /g++ Why some variables are not detected as not used during compilation?
- gcc / g++ preprocessor flags for the compiler version number
- gcc / g++ how to dump all preprocessor definitions
- gcc / g++ find where a header file is included from
- g++ compilation option -Weffc++
- Disable all gcc / g++ warnings
- Code that compiles with gcc but not g++
- Build in release mode with full optimizations in gcc / g++
- C++ - Faq C++ GCC G++