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

标签 c++ c xcode macos macros

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

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

Porting Command Line Unix Tools to Mac OS X guides 指定了 __APPLE__ 和另外的 __APPLE_CC__ 但没有没有提及 __OSX__

Porting from GCC指南说:

  • Use #ifdef __GNUC__ to wrap any GCC-specific code.
  • Use #ifdef __APPLE_CC__ to wrap any Mac OS X-specific code.

同样,没有提及 __OSX__ 宏。

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

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

最佳答案

这一切都取决于。

每个宏都指定了不同的含义。
见: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__ 来区分特定于苹果的代码。

关于c++ - 用 C/C++ 包装 Mac OS X 特定代码的宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2166483/

相关文章:

iphone - 自定义 segue 推像后退按钮

c++ - 分配大量内存会出错?为什么?

c++ - 如何禁止分配给不引用变量?

c - 编程 Linux 应用程序以同时播放多个声音

c - 内存分配的递归

swift - 在 SwiftUI 中同时打开多个预览

ios - 架构 armv7 的 2500 多个重复符号 - Xcode 8

c# - 如何在 Windows 中透明地以管理员身份执行任务

c++ - 片段着色器中的 GLSL 翻译未按预期工作

c - C中的数组赋值