在阅读各种 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
特定的宏吗?
最佳答案
这一切都取决于。
__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/