macos 对于 OS X 特定代码,我应该使用什么 C 预处理器条件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1529031/
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-10-21 06:28:13  来源:igfitidea点击:

What C preprocessor conditional should I use for OS X specific code?

cmacosc-preprocessorconditional-compilation

提问by klynch

What C preprocessor conditional should I use for OS X specific code? I need to include a specific library if I am compiling for OS X or a different header if I am compiling for Linux.

对于 OS X 特定代码,我应该使用什么 C 预处理器条件?如果我为 OS X 编译,我需要包含一个特定的库,如果我为 Linux 编译,我需要包含一个不同的头文件。

I know there is __APPLE__but I don't know if that is a current conditional for OS X 10.x.

我知道有,__APPLE__但我不知道这是否是 OS X 10.x 的当前条件。

回答by Mark Rushakoff

This list of operating system macros says the presence of both__APPLE__and __MACH__indicate OSX.

此操作系统宏列表说明两者的存在__APPLE____MACH__指示 OSX。

Also confirmed at line 18 of part of the source for fdisk.

还在部分来源的fdisk第 18 行确认 .

回答by Mark Bessey

__APPLE__will tell you you're compiling on an Apple platform. Unless you need to support MacOS versions before OS X, that should be good enough. Alternately, you could use __APPLE__and __MACH__to make sure you're compiling on OS X.

__APPLE__会告诉你你正在 Apple 平台上编译。除非您需要支持 OS X 之前的 MacOS 版本,否则应该足够了。或者,您可以使用__APPLE____MACH__确保您在 OS X 上进行编译。

回答by Thibault Martin-Lagardette

If I remember correctly, it's __APPLE__:)

如果我没记错的话,是__APPLE__:)

回答by Naseef Ur Rahman

This code example may help you -

此代码示例可能会帮助您 -

if defined(__APPLE__)
#include "TargetConditionals.h"
  if (!defined(TARGET_OS_IPHONE) && !defined(TARGET_IPHONE_SIMULATOR))
 {
   //write your OSX specific code here
 } 

回答by ingconti

old style raw:

老式原始:

#ifdef WIN32
// windows.
#elif __APPLE__
// osx and ios.
#endif

回答by user772401

This pagecontains a list of all OS predefined macros.

此页面包含所有操作系统预定义宏的列表。

For mac OSX both the __APPLE__&& __MACH__need to be defined.

对于 mac OSX,两个__APPLE__&& 都__MACH__需要定义。