用 C/C++ 包装 Mac OS X 特定代码的宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2166483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:21:54  来源:igfitidea点击:

Which macro to wrap Mac OS X specific code in C/C++

c++cxcodemacosmacros

提问by mloskot

While reading various C and C++ sources, I have encountered two macros __APPLE__and __OSX__. I found plenty of use of __OSX__in various codes, especially those originating from *BSDsystems.

在阅读各种 C 和 C++ 源代码时,我遇到了两个宏__APPLE____OSX__. 我__OSX__在各种代码中发现了大量使用,尤其是那些源自*BSD系统的代码。

However, sometimes I find that testing __OSX__only is not sufficient and I have to complete tests with __APPLE__macro.

但是,有时我发现__OSX__仅测试是不够的,我必须使用__APPLE__宏完成测试。

The Porting Command Line Unix Tools to Mac OS Xguides specifies __APPLE__and additionally __APPLE_CC__but does notmention __OSX__.

移植Unix命令行工具到Mac OS X引导指定__APPLE__,此外__APPLE_CC__没有提及__OSX__

The Porting from GCCguide says:

从GCC移植指南说:

  • Use #ifdef __GNUC__to wrap any GCC-specific code.
  • Use #ifdef __APPLE_CC__to wrap any Mac OS X-specific code.
  • 使用#ifdef __GNUC__封装任意GCC特定的代码。
  • 使用#ifdef __APPLE_CC__封装任意Mac OS X的特定代码。

Again, no mention about __OSX__macro.

再次,没有提到__OSX__宏。

What macro is predefined on Mac OS X platform and XCode development environment that should be used to distinguish OSX-specific code in C/C++ programs?

Mac OS X 平台和 XCode 开发环境中预定义了哪些宏,用于区分 C/C++ 程序中的 OSX 特定代码?

Where is the __OSX__macro defined? Is it *BSDspecific macro?

__OSX__宏在哪里定义的?是*BSD特定的宏吗?

采纳答案by Martin York

It all depends.

这一切都取决于。

Each macro specifies something different in meaning.
See: https://developer.apple.com/library/mac/documentation/Porting/Conceptual/PortingUnix/compiling/compiling.html#//apple_ref/doc/uid/TP40002850-SW13

每个宏指定了不同的含义。
参见:https: //developer.apple.com/library/mac/documentation/Porting/Conceptual/PortingUnix/compiling/compiling.html#//apple_ref/doc/uid/TP40002850-SW13

__APPLE__

This macro is defined in any Apple computer.

__APPLE_CC__

This macro is set to an integer that represents the version number of the compiler. This lets you distinguish, for example, between compilers based on the same version of GCC, but with different bug fixes or features. Larger values denote later compilers.

__OSX__

Presumably the OS is a particular variant of OS X

__APPLE__

此宏在任何 Apple 计算机中都有定义。

__APPLE_CC__

这个宏被设置为一个整数,代表编译器的版本号。例如,这使您可以区分基于相同 GCC 版本但具有不同错误修复或功能的编译器。较大的值表示较晚的编译器。

__OSX__

据推测,该操作系统是 OS X 的特定变体

So given the above definitions I would use __APPLE__to distinguish apple specific code.

因此,鉴于上述定义,我将用于__APPLE__区分 Apple 特定代码。

回答by Kornel Kisielewicz

Here is a nice listof macros for operating systems.

这是一个很好的操作系统宏列表

There's little info on __OSX__on the web. You'll be safe with __APPLE__.

__OSX__网上的资料很少。你会安全的__APPLE__

回答by Paul R

I normally use __MACH__for this. It's been defined since the earliest version of OS X (and even before, presumably).

我通常__MACH__用于此。它从最早的 OS X 版本开始就被定义了(大概甚至更早)。

回答by scravy

Use

#if defined(__APPLE__) && defined(__MACH__)

to distinguish Apple MacOS (not iOS).

来区分 Apple MacOS(不是 iOS)。

Regarding the "where does OSXcome from":

关于“ OSX来自哪里”:

Some on-line lists of compiler macros (like this one) list __MACOSX__. Some forum comments (like these) claim __OSX__exists. These are incorrect. There are no such macros predefined by OSX compilers, but they may be defined by specific project Makefiles and platform-detector scripts like GNU autoconf.

一些编译器宏的在线列表(比如这个) list __MACOSX__。一些论坛评论(如这些)声称__OSX__存在。这些都是不正确的。OSX 编译器没有预定义这样的宏,但它们可能由特定的项目 Makefile 和平台检测器脚本(如 GNU autoconf)定义。

Source: http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system

来源:http: //nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system

回答by SMGreenfield

For anyone coming across this question >= 2019, I found there's a header "Availability.h".

对于遇到此问题的任何人 >= 2019,我发现有一个标题“Availability.h”。

In that header, are #defines like:

在该标题中,#defines 如下:

#define __MAC_10_0            1000
#define __MAC_10_1            1010
#define __MAC_10_2            1020
#define __MAC_10_3            1030
#define __MAC_10_4            1040
#define __MAC_10_5            1050
#define __MAC_10_6            1060
#define __MAC_10_7            1070
#define __MAC_10_8            1080
#define __MAC_10_9            1090
#define __MAC_10_10         101000
#define __MAC_10_10_2       101002
#define __MAC_10_10_3       101003
#define __MAC_10_11         101100
#define __MAC_10_11_2       101102

So you CAN tell if you're compiling on a particular MacOS platform.

因此,您可以判断您是否在特定的 MacOS 平台上进行编译。

回答by prewett

See http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system#OSXiOSandDarwin

http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system#OSXiOSandDarwin

#ifdef __APPLE__
#include <TargetConditionals.h>
#if TARGET_OS_MAC
   ...
#endif /* TARGET_OS_MAC */
#endif /* __APPLE__ */

Note that __OSX__does NOTexist, at least as of Xcode 9.

需要注意的是__OSX__存在,至少在Xcode中9。

Also note that it is #if TARGET_OS_MACnot #ifdef. It is always defined, but is 0 when not macOS.

另请注意,它#if TARGET_OS_MAC不是#ifdef. 它始终被定义,但在 macOS 之外时为 0。