Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the gcc predefined macros for the compiler's version number?

I have run into a bug with gcc v3.4.4 and which to put an #ifdef in my code to work around the bug for only that version of the compiler.

What are the GCC compiler preprocessor predefined macros to detect the version number of the compiler?

like image 597
WilliamKF Avatar asked Dec 20 '09 19:12

WilliamKF


People also ask

What are the predefined macros?

Predefined macros are those that the compiler defines (in contrast to those user defines in the source file). Those macros must not be re-defined or undefined by user.

What version of GCC supports C ++ 11?

Status of Experimental C++11 Support in GCC 4.8 GCC provides experimental support for the 2011 ISO C++ standard. This support can be enabled with the -std=c++11 or -std=gnu++11 compiler options; the former disables GNU extensions.

What is the current GCC version?

The current version is GCC 7.3, released on 2018-01-25. GCC is a key component of so-called "GNU Toolchain", for developing applications and writing operating systems. The GNU Toolchain includes: GNU Compiler Collection (GCC): a compiler suite that supports many languages, such as C/C++ and Objective-C/C++.


1 Answers

From the gnu cpp manual...


__GNUC__ __GNUC_MINOR__ __GNUC_PATCHLEVEL__ 

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 __GNUC__ to 3, __GNUC_MINOR__ to 2, and __GNUC_PATCHLEVEL__ to 1. These macros are also defined if you invoke the preprocessor directly.

__GNUC_PATCHLEVEL__ 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 __GNUC__. 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:

          /* Test for GCC > 3.2.0 */           #if __GNUC__ > 3 || \               (__GNUC__ == 3 && (__GNUC_MINOR__ > 2 || \                                  (__GNUC_MINOR__ == 2 && \                                   __GNUC_PATCHLEVEL__ > 0))) 
like image 119
DigitalRoss Avatar answered Sep 25 '22 08:09

DigitalRoss