C和C++标准、平台、编译器、库相关预定义宏

在写代码中有时需要适配不同的编译器和平台,一般通过判断编译器预定义的宏来确定所需信息。

这些信息可在以下网站找到:

  1. Pre-defined Compiler Macros wiki - GitHub
    Pre-defined Compiler Macros - SourceForge
    (二者内容完全相同)
  2. 2 Language Standards Supported by GCC (Using the GNU Compiler Collection (GCC))
  3. Predefined Macros (The C Preprocessor)
  4. Extensions to the C Language Family
    1. 扩展数据类型
      1. 128-bit Integers
      2. Double-Word Integers (Long Long);
      3. Half-Precision Floating Point
      4. Decimal Floating Types
    2. 属性
      1. Declaring Attributes of Functions
      2. Specifying Attributes of Variables
      3. Specifying Attributes of Types
      4. ...

包括——

  • 使用的标准
    • 语言标准(如C99、C++11等)
    • POSIX标准
    • XPG标准
    • ……
  • 编译器信息
    • 编译器名称(如GCC、ARMCC、Clang、MSVC等)
    • 编译器版本
  • 编译器使用的库的信息
    • 使用的C基础库与C++基础库(如glibc、libstdc++等)
    • 上述基础库版本
  • 目标架构
    • 目标平台的架构(如x86-64、ARMv7、MIPS等)
    • 上述架构版本
  • 目标系统
    • 目标平台使用的系统(如Windows、Linux)
      • 注意,这些定义不一定互斥,比如:由于Android基于Linux,因此Android平台包含Linux的定义
    • 上述系统版本(不一定有)
  • 编译器扩展
  • ……