如何在C预处理器中可靠地检测Mac OS X,iOS,Linux,Windows? [重复]
本文翻译自:How to detect reliably Mac OS X, iOS, Linux, Windows in C preprocessor? [duplicate]
This question already has an answer here: 这个问题已经在这里有了答案:
- How do I check OS with a preprocessor directive? 如何使用预处理指令检查OS? 16 answers 16个答案
If there's some cross-platform C/C++ code that should be compiled on Mac OS X, iOS, Linux, Windows, how can I detect them reliably during preprocessor process? 如果有一些跨平台的C / C ++代码应该在Mac OS X,iOS,Linux,Windows上编译,那么在预处理器过程中如何可靠地检测到它们?
#1楼
参考:https://stackoom.com/question/Oq3o/如何在C预处理器中可靠地检测Mac-OS-X-iOS-Linux-Windows-重复
#2楼
As Jake points out, TARGET_IPHONE_SIMULATOR
is a subset of TARGET_OS_IPHONE
. 正如Jake指出的那样, TARGET_IPHONE_SIMULATOR
是TARGET_OS_IPHONE
的子集。
Also, TARGET_OS_IPHONE
is a subset of TARGET_OS_MAC
. 此外, TARGET_OS_IPHONE
的一个子集TARGET_OS_MAC
。
So a better approach might be: 因此,更好的方法可能是:
#ifdef _WIN64//define something for Windows (64-bit)
#elif _WIN32//define something for Windows (32-bit)
#elif __APPLE__#include "TargetConditionals.h"#if TARGET_OS_IPHONE && TARGET_IPHONE_SIMULATOR// define something for simulator #elif TARGET_OS_IPHONE// define something for iphone #else#define TARGET_OS_OSX 1// define something for OSX#endif
#elif __linux// linux
#elif __unix // all unices not caught above// Unix
#elif __posix// POSIX
#endif
#3楼
Kind of a corollary answer: the people on [this site] have taken the time to make tables of macros defined for every OS/compiler pair . 一个必然的答案: [此站点]上的人花了一些时间为每个OS /编译器对定义宏表。
For example, you can see that _WIN32
is NOT defined on Windows with Cygwin (POSIX), while it IS defined for compilation on Windows, Cygwin (non-POSIX), and MinGW with every available compiler (Clang, GNU, Intel, etc.). 例如,您可以看到_WIN32
在Windows上未使用Cygwin(POSIX)定义,而它是在Windows,Cygwin(非POSIX)和MinGW上使用每个可用的编译器(Clang,GNU,Intel等)定义的。 )。
Anyway, I found the tables quite informative and thought I'd share here. 无论如何,我发现这些表非常有用,并认为我会在这里分享。
#4楼
There are predefined macros that are used by most compilers, you can find the list here . 大多数编译器都使用预定义的宏,您可以在此处找到列表。 GCC compiler predefined macros can be found here . GCC编译器预定义的宏可以在这里找到。 Here is an example for gcc: 这是gcc的示例:
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)//define something for Windows (32-bit and 64-bit, this part is common)#ifdef _WIN64//define something for Windows (64-bit only)#else//define something for Windows (32-bit only)#endif
#elif __APPLE__#include <TargetConditionals.h>#if TARGET_IPHONE_SIMULATOR// iOS Simulator#elif TARGET_OS_IPHONE// iOS device#elif TARGET_OS_MAC// Other kinds of Mac OS#else# error "Unknown Apple platform"#endif
#elif __linux__// linux
#elif __unix__ // all unices not caught above// Unix
#elif defined(_POSIX_VERSION)// POSIX
#else
# error "Unknown compiler"
#endif
The defined macros depend on the compiler that you are going to use. 定义的宏取决于您将要使用的编译器。
The _WIN64
#ifdef
can be nested into the _WIN32
#ifdef
because _WIN32
is even defined when targeting the Windows x64 version. 该_WIN64
#ifdef
可以被嵌套到_WIN32
#ifdef
因为_WIN32
针对Windows的64位版本时,甚至定义。 This prevents code duplication if some header includes are common to both (also WIN32
without underscore allows IDE to highlight the right partition of code). 如果某些头文件包含了两者,这可以防止代码重复(同样,不带下划线的WIN32
也允许IDE突出显示正确的代码分区)。
如何在C预处理器中可靠地检测Mac OS X,iOS,Linux,Windows? [重复]相关推荐
- 想不到吧?Docker 中也能跑 Mac OS 拉!
点关注公众号,回复"1024"获取2TB学习资源! Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linu ...
- 如何在Azure machine learning中使用异常检测
https://www.toutiao.com/a6688176468599505412/ 2019-05-07 15:00:32 正如Azure最新的机器学习服务所显示的那样,机器学习已不再仅仅是关 ...
- Qt文档阅读笔记-单元测试中构建标准检测程序(CPU、Linux性能、内存泄漏等)
这里需要介绍一个宏,主要就是靠使用这个宏完成功能: QBENCHMARK{//TODO } 在单元测试中,这个宏在那个函数中,那么这个函数就会进行多次测试,如果不需要多次调用可以用下面的这宏代替: Q ...
- 如何在VMware Workstation虚拟机上安装苹果系统(Mac OS)
01. 准备工具 VMware Workstation 12 Mac OS python-2.7.9.amd64 unlocker206 02. 步骤 2.1 下载VMware Workstation ...
- 您详细比较三个 CSS 预处理器(框架):Sass、LESS 和 Stylus
2019独角兽企业重金招聘Python工程师标准>>> CSS 预处理器技术已经非常的成熟,而且也涌现出了越来越多的 CSS 的预处理器框架.本文向你介绍使用最为普遍的三款 CSS ...
- 原生 CSS “杀死” 预处理器 Sass!
[CSDN 编者按]有着世界上最成熟.稳定和强大的专业级 CSS 扩展语言之称的 Sass,是 CSS 预处理器中的一种,其可编程能力较强,支持函数.列表.对象.判断.循环等等,凭借这些优势,其一度成 ...
- CSS预处理器语言:Sass、LESS、Stylus
CSS预处理器语言:Sass.LESS.Stylus Sass 和 Less 都使用的是标准的 CSS 语法.默认 Sass 使用 .sass 扩展名, Less 使用 .less 扩展名,Stylu ...
- CSS预处理器——Sass、LESS和Stylus区别及联系
一.什么是CSS预处器 CSS预处理器定义了一种新的语言,其基本思想是,用一种专门的编程语言,为CSS增加了一些编程的特性,将CSS作为目标生成文件,然后开发者就只要使用这种语言进行编码工作.通俗的说 ...
- CSS预处理器-Sass、Less和Stylus-介绍和使用
发挥CSS预处器的作用是一种很有挑战性的事情.CSS预处器有不同的语言,有不同的语法和功能. 在这篇文章中,我们将介绍三种不同CSS预处器的蛮量.功能以及他们的好处--sass.less 和stylu ...
最新文章
- 来自Facebook AI的多任务多模态的统一Transformer:向更通用的智能迈出了一步
- 【控制】第九章-线性系统的状态空间描述
- MSVCRTD.lib(crtexe.obj) : error LNK2019
- VC星号密码查看器源码
- Go语言讲解深拷贝与浅拷贝
- commons-csv 使用记叙
- select2控件动态更新option
- idea退出首界面_如何取消Idea开始界面打开默认项目配置
- java 调用 rsync_运行rsync时,QProcess不显示任何输出
- java创建对象实例的四种方式
- [CareerCup] 18.8 Search String 搜索字符串
- 【验证码识别】基于matlab CNN卷积神经网络验证码识别【含Matlab源码 098期】
- 如何解决unity使用vs2017、vs2018、vs2019等 编辑器创建新项目时无法产生.sln文件的一个小办法
- oracle 总僵尸进程,subprocess子进程kill后存在僵尸进程的原因及处理方法
- python3写的腾讯漫画下载器
- QT视频采集之编码Enc和录像Rec
- 仓库温度湿度控制措施_一般仓库的温湿度控制范围是多少合适?
- 第3章 远程连接管理Linux实践
- 京东API接口JD商品详情
- 全国中小学教师计算机水平考试,中小学教师计算机水平考试笔试试题