2453|0

6040

帖子

195

TA的资源

版主

楼主
 

【转】c编译器版本和系统判断的预定义宏 [复制链接]

本帖最后由 lcofjp 于 2020-5-4 18:02 编辑

When writing portable C++ code you need to write conditional code that depends on compiler used or the OS for which the code is written.

Here’s a typical case:

#if defined (_MSC_VER)
// code specific to Visual Studio compiler
#endif

To perform those checks you need to check pre-processor macros that various compilers set.

It can eihter be binary is defined vs. is not defined check (e.g. __APPLE__) or checking a value of the macro (e.g. _MSC_VER defines version of Visual Studio compiler).

This document describes macros set by various compilers.

Other documentations:

Checking for OS (platform)

To check for which OS the code is compiled:

Linux and Linux-derived           __linux__
Android                           __ANDROID__ (implies __linux__)
Linux (non-Android)               __linux__ && !__ANDROID__
Darwin (Mac OS X and iOS)         __APPLE__
Akaros (http://akaros.org)        __ros__
Windows                           _WIN32
Windows 64 bit                    _WIN64 (implies _WIN32)
NaCL                              __native_client__
AsmJS                             __asmjs__
Fuschia                           __Fuchsia__

Checking the compiler:

To check which compiler is used:

Visual Studio       _MSC_VER
gcc                 __GNUC__
clang               __clang__
emscripten          __EMSCRIPTEN__ (for asm.js and webassembly)
MinGW 32            __MINGW32__
MinGW-w64 32bit     __MINGW32__
MinGW-w64 64bit     __MINGW64__

Checking compiler version

gcc

__GNUC__ (e.g. 5) and __GNUC_MINOR__ (e.g. 1).

To check that this is gcc compiler version 5.1 or greater:

#if defined(__GNUC__) && (__GNUC___ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1))
// this is gcc 5.1 or greater
#endif

Notice the chack has to be: major > 5 || (major == 5 && minor >= 1). If you only do major == 5 && minor >= 1, it won’t work for version 6.0.

clang

__clang_major____clang_minor____clang_patchlevel__

Visual Studio

_MSC_VER and _MSC_FULL_VER:

VS                        _MSC_VER   _MSC_FULL_VER
1                         800
3                         900
4                         1000
4                         1020
5                         1100
6                         1200
6 SP6                     1200    12008804
7                         1300    13009466
7.1 (2003)                1310    13103077
8 (2005)                  1400    140050727
9 (2008)                  1500    150021022
9 SP1                     1500    150030729
10 (2010)                 1600    160030319
10 (2010) SP1             1600    160040219
11 (2012)                 1700    170050727
12 (2013)                 1800    180021005
14 (2015)                 1900    190023026
14 (2015 Update 1)        1900    190023506
14 (2015 Update 2)        1900    190023918
14 (2015 Update 3)        1900    190024210
15 (2017 Update 1 & 2)    1910    191025017
15 (2017 Update 3 & 4)    1911
15 (2017 Update 5)        1912

More information:

MinGW

MinGW (aka MinGW32) and MinGW-w64 32bit: __MINGW32_MAJOR_VERSION and __MINGW32_MINOR_VERSION

MinGW-w64 64bit: __MINGW64_VERSION_MAJOR and __MINGW64_VERSION_MINOR

Checking processor architecture

gcc

The meaning of those should be self-evident: 

  • __i386__
  • __x86_64__
  • __arm__. If defined, you can further check:
    • __ARM_ARCH_5T__
    • __ARM_ARCH_7A__
  • __powerpc64__
  • __aarch64__

 

 


原文地址:https://blog.kowalczyk.info/article/j/guide-to-predefined-macros-in-c-compilers-gcc-clang-msvc-etc..html

文章中的链接可以作为进一步的参考。

 

此帖出自编程基础论坛
点赞 关注
 

回复
举报
您需要登录后才可以回帖 登录 | 注册

查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/8 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表