• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C/C++预定义编译宏

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Pre-defined C/C++ Compiler Macros

https://sourceforge.net/p/predef/wiki/Home/

The macros are found here:

General guidelines are found here:

Introduction

C and C++ compilers automatically define certain macros that can be used to check for compiler or operating system features. This is useful when writing portable software.

These pages lists various pre-defined compiler macros that can be used to identify standards, compilers, operating systems, hardware architectures, and even basic run-time libraries at compile-time.

For example, if we want to use a generic or opaque pointer type, we use void pointers. However, ancient K&R compilers (from the time before the first ANSI C standard) do not support void pointers. Instead we can define our own type:

#if defined(__STDC__) || defined(__cplusplus) || defined(_MSC_EXTENSIONS)

typedef void * t_pointer;

#else

typedef char * t_pointer;

#endif

Another example, Microsoft Visual C++ version 4.2 added a pragma to reduce compilation times by only including a file once (if _MSC_VER is not defined then it will evaluate to 0 (zero) — however, some compilers may complain about an undefined macro)

#if defined(_MSC_VER) && (_MSC_VER >= 1020)

#pragma once

#endif

The macros contained in these pages have been obtained through vendor documentation, the defines script, contributors, and third-party source code. No guarantee about the correctness of the macros is given.

An often-used alternative is Autoconf, which is a more powerful tool to examine various types of features, including compilation options. However, Autoconf is fairly Unix-centric, and requires a Unix layer on other platforms (e.g. Cygwin on Windows). Other alternatives are BuildtoolCMakeSConsPMKJamAnt, and Bakefile.

Contributors

Bjorn Reese, Daniel Stenberg, Greg Roelofs, Steven G. Johnson, Wlodzimierz ABX Skiba, Marc Finet, Philip Newton, Mitchell Charity, Christian Klutz, Seo Sanghyeon, Chris Adami, Geoff Clare, Dan Fandrich, Mike Gorchak, Yuri D'Elia, Gynvael Coldwind, Alain Tauch, Vadim Zeitlin, Steve White, Thomas David Rivers, Tom Honermann, Martin Mitas, Dinesh Chhadwa, Erik Faye-Lund, Leo Davis, Paul Hsieh, Roland Schwarz, Darko Kolakovic, Andy Buonviri, Ming Kin Lai, Kent Johnson, Helmut Bauer, Oliver Schneider, Ron Pimblett, Jose Luis Rodriguez Garcia, Jeroen Ruigrok van der Werven, Uffe Jakobsen, Bryan Ashby, Bruno Haible, Artur Bac, Terry Schwarz, Leo Davis, Markus Duft, William Dang, Paul Green, Ruben Van Boxem, Pau Garcia i Quiles, Mikulas Patocka, Leo Davis, Mark Ferry, Holger Machens, Simon Watts, Paul Hargrove, Hans-Christoph Steiner, Gerald Combs, Denys Bulant, Massimo Morara, Jeremy Bennett, Guillem Jover, Riku Voipio, Jacques Pelletier, Mark Jarvin, Georg Sauthoff, Scot Jenkins, Grzegorz Brzęczyszczykiewicz, John Dallman, Gianmichele Toglia, Robbie Groenewoudt, Andreas Mohr, Алексей Пюрецкий, Sverre Hvammen Johansen, Stefan Tauner, Daniel Garcia, Ozkan Sezer, Dean Saridakis, Frank Long, Kevin Adler.

Top

Language Standards

Language standards requires the existence of pre-defined macros.

Name

Macro

Standard

C89

__STDC__

ANSI X3.159-1989

C90

__STDC__

ISO/IEC 9899:1990

C94

__STDC_VERSION__ = 199409L

ISO/IEC 9899-1:1994

C99

__STDC_VERSION__ = 199901L

ISO/IEC 9899:1999

C11

__STDC_VERSION__ = 201112L

ISO/IEC 9899:2011

C++98

__cplusplus = 199711L

ISO/IEC 14882:1998

C++11

__cplusplus = 201103L

ISO/IEC 14882:2011

C++14

__cplusplus = 201402L

ISO/IEC 14882:2014

C++/CLI

__cplusplus_cli = 200406L

ECMA-372

DSP-C

 

ISO/IEC JTC1/SC22 WG14/N854

EC++

__embedded_cplusplus

Embedded C++

Example: C Standards
#if defined(__STDC__)
# define PREDEF_STANDARD_C_1989
# if defined(__STDC_VERSION__)
#  if (__STDC_VERSION__ >= 199409L)
#   define PREDEF_STANDARD_C_1994
#  endif
#  if (__STDC_VERSION__ >= 199901L)
#   define PREDEF_STANDARD_C_1999
#  endif
# endif
#endif

Notice that not all compliant compilers provides the correct pre-defined macros. For example, Microsoft Visual C++ does not define __STDC__, or Sun Workshop 4.2 supports C94 without setting __STDC_VERSION__ to the proper value. Extra checks for such compilers must be added.

Notice that some compilers, such as the HP aC++, use the value 199707L to indicate the C++98 standard. This value was used in an earlier proposal of the C++98 standard.

Example: Pre-C89

In continuation of the above example, pre-C89 compilers do not recognize certain keywords. Let the preprocessor remove those keywords for those compilers.

#if !defined(PREDEF_STANDARD_C_1989) && !defined(__cplusplus)
# define const
# define volatile
#endif

Unix Standards

There are several related Unix standards, such as POSIXX/Open, and LSB.

Unix standards require the existence macros in the <unistd.h> header file.

Name

Macro

Standard

POSIX.1-1988

_POSIX_VERSION = 198808L

 

POSIX.1-1990

_POSIX_VERSION = 199009L

ISO/IEC 9945-1:1990

POSIX.2

_POSIX2_C_VERSION = 199209L

ISO/IEC 9945-2:1993

POSIX.1b-1993

_POSIX_VERSION = 199309L

IEEE 1003.1b-1993

POSIX.1-1996

_POSIX_VERSION = 199506L

IEEE 1003.1-1996

POSIX.1-2001

_POSIX_VERSION = 200112L

IEEE 1003.1-2001

POSIX.1-2008

_POSIX_VERSION = 200809L

IEEE 1003.1-2008

XPG3

_XOPEN_VERSION = 3

X/Open Portability Guide 3 (1989)

XPG4

_XOPEN_VERSION = 4

X/Open Portability Guide 4 (1992)

SUS

_XOPEN_VERSION = 4 && _XOPEN_UNIX

X/Open Single UNIX Specification (UNIX95)

SUSv2

_XOPEN_VERSION = 500

X/Open Single UNIX Specification, Version 2 (UNIX98)

SUSv3

_XOPEN_VERSION = 600

Open Group Single UNIX Specification, Version 3 (UNIX03)

SUSv4

_XOPEN_VERSION = 700

Open Group Single UNIX Specification, Version 4

LSB

__LSB_VERSION__ = VR

Linux Standards Base

V = Version
R = Revision

Example: Unix Standards

The following examples assumes the definition of these macros.

#if defined(unix) || defined(__unix__) || defined(__unix)
# define PREDEF_PLATFORM_UNIX
#endif
#if defined(PREDEF_PLATFORM_UNIX)
# include <unistd.h>
# if defined(_XOPEN_VERSION)
#  if (_XOPEN_VERSION >= 3)
#   define PREDEF_STANDARD_XOPEN_1989
#  endif
#  if (_XOPEN_VERSION >= 4)
#   define PREDEF_STANDARD_XOPEN_1992
#  endif
#  if (_XOPEN_VERSION >= 4) && defined(_XOPEN_UNIX)
#   define PREDEF_STANDARD_XOPEN_1995
#  endif
#  if (_XOPEN_VERSION >= 500)
#   define PREDEF_STANDARD_XOPEN_1998
#  endif
#  if (_XOPEN_VERSION >= 600)
#   define PREDEF_STANDARD_XOPEN_2003
#  endif
#  if (_XOPEN_VERSION >= 700)
#   define PREDEF_STANDARD_XOPEN_2008
#  endif
# endif
#endif

Notice that not all compliant compilers provides the correct pre-defined macros. For example, IBM xlC supports Unix without setting any of the __unix__ macros. Extra checks for such compilers must be added.

Top

Compilers  

ACC

Type

Macro

Identification

_ACC_

Altium MicroBlaze C

Type

Macro

Format

Description

Identification

__CMB__

   

Version

__VERSION__

VRRR

V=Version
RRR=Revision

Version

__REVISION__

P

P=Patch

Version

__BUILD__

VRRRPPP

Build number

Example

Altium MicroBlaze C

__VERSION__

__REVISION__

__BUILD__

1.0r2

1000

2

 

1.22.2

   

1022001

Altium C-to-Hardware

Type

Macro

Format

Description

Identification

__CHC__

   

Version

__VERSION__

VRRR

V=Version
RRR=Revision

Version

__REVISION__

P

P=Patch
Beta releases set this to -1

Version

__BUILD__

VRRRPPP

Build number

Example

Altium C-to-Hardware

__VERSION__

__REVISION__

__BUILD__

2.1r1

2001

1

 

1.22.2

   

1022001

Amsterdam Compiler Kit

Type

Macro

Identification

__ACK__

ARM Compiler

Type

Macro

Format

Description

Identification

__CC_ARM

   

Version

__ARMCC_VERSION

VRPBBB

V = Version
R = Revision
P = Patch
BBB = Build

Notice that the __ARMCC_VERSION macro is also used as version indicator for Norcroft C, but that the format is different.

Example

Realview C

__ARMCC_VERSION

3.0

300503

Aztec C

Type

Macro

Format

Description

Identification

AZTEC_C
__AZTEC_C__

   

Version

__VERSION

VRR

V = Version
RR = Revision

Example

Aztec C

__VERSION

5.20

520

Borland C++

Type

Macro

Format

Identification

__BORLANDC__

 

Version

__BORLANDC__

?

Identification

__CODEGEARC__

 

Version

__CODEGEARC__

From C++ Builder 2006

Example

Borland C++

C++ Builder

__BORLANDC__

__CODEGEARC__

2.0

 

0x200

 

3.0

 

0x400

 

3.1

 

0x410

 

4.0

 

0x452

 

5.0

 

0x500

 

5.02

1.0

0x520

 
 

3.0

0x530

 
 

4.0

0x540

 

5.5

5.0

0x550

 

5.51

 

0x551

 

5.6.4

 

0x562

 
 

2006

0x570

0x570

 

2007

0x590

0x590

 

2009

0x613

0x613

 

2010

0x621

0x621

 

XE

0x630

0x630

 

XE2

0x640

0x640

 

XE3

0x650

0x650

 

XE4

0x660

0x660

CC65

Type

Macro

Format

Description

Identification

__CC65__

   

Version

__CC65__

0xVRP

V = Version
R = Revision
P = Patch

Example

Version

__CC65__

2.10.1

0x2A1

Clang

Type

Macro

Format

Identification

__clang__

 

Version

__clang_major__

Version

Version

__clang_minor__

Revision

Version

__clang_patchlevel__

Patch

Notice that clang also defines the GNU C version macros, but you should use the clang feature checking macros to detect the availability of various features.

The values of the __clang_major____clang_minor__, and __clang_patchlevel__ macros are not consistent across distributions of the Clang compiler. For example, the Clang 3.1 distribution available at http://clang.llvm.org defines __clang_major__ and __clang_minor__ as 3 and 1 respectively. The version of Clang distributed with Apple XCode 4.4 is branded as "Apple Clang 4.0" and derives from the open source Clang 3.1 distribution, but defines these macros with the values 4 and 0 respectively. Apple's Clang distribution can be identified by the presence of the __apple_build_version__macro.

Comeau C++

Type

Macro

Format

Description

Identification

__COMO__

   

Version

__COMO_VERSION__

VRR

V = Version
RR = Revision

Example

Comeau C++

__COMO_VERSION__

2.3

230

Compaq C/C++

Type

Macro

Format

Description

Identification

__DECC

 

C compiler

Version

__DECC_VER

VVRRTPPPP

VV = Version
RR = Revision
T = Type (9 = official)
PPPP = Patch

Identification

__DECCXX

 

C++ compiler

Version

__DECCXX_VER

As __DECC_VER

 

Identification

__VAXC

 

Obsolete

Identification

VAXC

 

Obsolete

Example

Compaq C/C++

__DECC_VER

6.0-001

60090001

Convex C

Type

Macro

Identification

__convexc__

CompCert

Type

Macro

Identification

__COMPCERT__

Coverity C/C++ Static Analyzer

Type

Macro

Identification

__COVERITY__

Cray C

Type

Macro

Description

Identification

_CRAYC

 

Version

_RELEASE

Version

Version

_RELEASE_MINOR

Revision

Diab C/C++

Type

Macro

Format

Description

Identification

__DCC__

1

 

Version

__VERSION_NUMBER__

VRPP

V = Version
R = Revision
PP = Patch

Example

Diab C/C++

__VERSION_NUMBER__

4.4g

4426

DICE C

Type

Macro

Identification

_DICE

Digital Mars

Type

Macro

Format

Description

Identification

__DMC__

   

Version

__DMC__

0xVRP

V = Version
R = Revision
P = Patch

Example

Digital Mars

__DMC__

7.0

0x700

7.2

0x720

8.0

0x800

Dignus Systems/C++

Type

Macro

Format

Description

Identification

__SYSC__

   

Version

__SYSC_VER__

VRRPP

V = Version
RR = Revision
PP = Patch

Example

Systems/C

__SYSC_VER__

1.80.32

18032

DJGPP

Type

Macro

Description

Identification

__DJGPP__

 

Version

__DJGPP__

Version

Version

__DJGPP_MINOR__

Revision

Identification

__GO32__

Defined by DJGPP 1.x

Example

DJGPP

__DJGPP__

__DJGPP_MINOR__

2.01

2

1

EDG C++ Frontend

Type

Macro

Format

Description

Identification

__EDG__

   

Version

__EDG_VERSION__

VRR

V = Version
RR = Revision

Example

EDG C++

__EDG_VERSION__

2.30

230

2.45

245

EKOPath

Type

Macro

Description

Identification

__PATHCC__

 

Version

__PATHCC__

Version

Version

__PATHCC_MINOR__

Revision

Version

__PATHCC_PATCHLEVEL__

Patch

Example

EKOPath

__PATHCC__

__PATHCC_MINOR__

__PATHCC_PATCHLEVEL__

2.0

2

0

0

Fujitsu C++

Type

Macro

Identification

__FCC_VERSION

GCC C/C++

Type

Macro

Description

Identification

__GNUC__

 

Version

__GNUC__

Version

Version

__GNUC_MINOR__

Revision

Version

__GNUC_PATCHLEVEL__

Patch (introduced in version 3.0)

Notice that the meaning of the __GNUC__ macro has changed subtly over the years, from identifying the GNU C/C++ compiler to identifying any compiler that implements the GNU compiler extensions (see the Feature request - a macro defined for GCC discussion for further information). For example, the Intel C++ on Linux also defines these macros from version 8.1 (see the Intel C++ Compiler 8.1 for Linux Release Notes and Intel Compilers for Linux: Compatibility with GNU Compilers.)

Example

GNU C/C++

__GNUC__

__GNUC_MINOR__

__GNUC_PATCHLEVEL__

2.7.x

2

7

N/A

3.0.2

3

0

2

Alternative Version

If you prefer a single version macro, you can define the following yourself.

#if defined(__GNUC__)
# if defined(__GNUC_PATCHLEVEL__)
#  define __GNUC_VERSION__ (__GNUC__ * 10000 \
                            + __GNUC_MINOR__ * 100 \
                            + __GNUC_PATCHLEVEL__)
# else
#  define __GNUC_VERSION__ (__GNUC__ * 10000 \
                            + __GNUC_MINOR__ * 100)
# endif
#endif

The format of this new macro is:

Type

Macro

Format

Description

Version

__GNUC_VERSION__

VVRRPP

VV = Version
RR = Revision
PP = Patch

Example of Alternative Version

GNU C/C++

__GNUC_VERSION__

2.7.x

20700

3.0.2

30002

Green Hill C/C++

Type

Macro

Format

Description

Identification

__ghs__

   

Version

__GHS_VERSION_NUMBER__

VRP

V = Version
R = Revision
P = Patch

Version

__GHS_REVISION_DATE__

Epoch time

 
Example

Green Hill C/C++

__GHS_VERSION_NUMBER__

4.2.1

421

HP ANSI C

Type

Macro

Identification

__HP_cc

HP aC++

Type

Macro

Format

Description

Identification

__HP_aCC

   

Version

__HP_aCC

1

From version A.01.15 (and A.03.13)

Version

__HP_aCC

VVRRPP

VV = Version
RR = Revision
PP = Patch

From version A.01.21 (and A.03.25)

Example

HP aCC

__HP_aCC

A.01.21

12100

IAR C/C++

Type

Macro

Format

Description

Identification

__IAR_SYSTEMS_ICC__

   

Version

__VER__

VRR

V = Version
RR = Revision

Example

IAR C/C++

__VER__

3.34

334

IBM XL C/C++

Type

Macro

Format

Description

Identification

__xlc__

   

Identification

__xlC__

   

Version

__xlC__

0xVVRR

VV = Version
RR = Revision

Version

__xlC_ver__

0x0000PPBB

PP = Patch
BB = Build

Identification

__IBMC__

 

From ?

Identification

__IBMCPP__

 

From ?

Version

__IBMC__
__IBMCPP__

VRP

V = Version
R = Revision
P = Patch

Notice that the z/OS C/C++ compiler also defines __IBMC__ and __IBMCPP__ macros, but with a different syntax. See the entry on the z/OS C/C++ compiler below for further information.

Example

IBM XL C/C++

__xlC__

__xlC_ver__

3.1

0x0301

 

4.5

0x0405

 

5.0

0x0500

 

12.01.0000.0004

0x0C01

0x00000004

IBM z/OS C/C++

This is the XL C/C++ compiler for mainframes (e.g. z/OS). The entry on XL C/C++ has been split into two for clarity.

Type

Macro

Format

Description

Identification

__IBMC__

   

Identification

__IBMCPP__

   

Version

__IBMC__
__IBMCPP__

NVRRM

N = Product (0 = C/370, 1 = MVS, 2 = OS/390, 4 = z/OS)
V = Version
RR = Revision
P = Patch

Defined for z/OS XL C/C++

Version

__COMPILER_VER__

0xNVRRPPPP

N = Product (see above)
V = Version
RR = Revision
PPPP = Patch

Defined for z/OS XL C/C++

Notice that XL C/C++ also defines __IBMC__ and __IBMCPP__ macros, but with a different syntax. You can use __xlC__ (only defined for XL C/C++) or __COMPILER_VER__ (only defined for z/OS C/C++) to distinguish between the two. Alternatively, the macro identifying z/OS (__MVS__) can be used to distinguish between them.

#if defined(__IBMC__) || defined(__IBMCPP__)
# if defined(__COMPILER_VER__)
/* z/OS C/C++ so __IBMC__ is defined as NVRRM */
# else
/* XL C/C++ so __IBMC__ is defined as VRP */
# endif
#endif
Example

IBM z/OS XL C/C++

__IBMC__

__COMPILER_VER__

1.7

41070

0x41070000

1.13

41130

0x410D0000

ImageCraft C

Type

Macro

Identification

__IMAGECRAFT__

Intel C/C++

Type

Macro

Format

Description

Identification

__INTEL_COMPILER

   

Identification

__ICC

 

Obsolete

Identification

__ECC

 

Obsolete

Identification

__ICL

   

Version

__INTEL_COMPILER

VRP

V = Version
R = Revision
P = Patch

Version

__INTEL_COMPILER_BUILD_DATE

YYYYMMDD

YYYY = Year
MM = Month
DD = Day

Example

Intel C/C++

__INTEL_COMPILER

__INTEL_COMPILER_BUILD_DATE

5.0

500

 

6.0

600

 

8.0

800

 

9.0

900

20060222

KAI C++

Type

Macro

Format

Description

Identification

__KCC

   

Version

__KCC_VERSION

0xVRPP

V = Version
R = Revision
PP = Patch (a = 01, b = 02, ...)

Example

KAI C++

__KCC_VERSION

4.0d

4004

KEIL CARM

Type

Macro

Format

Description

Identification

__CA__

   

Identification

__KEIL__

   

Version

__CA__

VRR

V = Version
RR = Revision

Example

Keil CARM

__CA__

1.01

101

KEIL C166

Type

Macro

Format

Description

Identification

__C166__

   

Version

__C166__

VRR

V = Version
RR = Revision

Example

Keil C166

__C166__

5.01

501

KEIL C51

Type

Macro

Format

Description

Identification

__C51__

   

Identification

__CX51__

   

Version

__C51__

VRR

V = Version
RR = Revision

Example

Keil C51

__C51__

7.01

701

LCC

Type

Macro

Identification

__LCC__

LLVM

Type

Macro

Identification

__llvm__

MetaWare High C/C++

Type

Macro

Identification

__HIGHC__


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#调用命令行删除文件及文件夹发布时间:2022-07-14
下一篇:
C#使用cookie实现登录发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap