macos 用于 GCC/G++ 的宏来区分 Linux 和 Mac OSX?

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

Macros for GCC/G++ to differentiate Linux and Mac OSX?

linuxmacosgcccross-platformmacros

提问by Viet

Macros for GCC/G++ to differentiate Linux and Mac OSX?

用于 GCC/G++ 的宏来区分 Linux 和 Mac OSX?

回答by themoondothshine

The next time you want to check out pre-defined macros supported by GCC on a platform, run the preprocessor with the flag -dM. It'll list out all the predefined macros available on the system. For example:

下次您想查看平台上 GCC 支持的预定义宏时,请运行带有标志的预处理器-dM。它将列出系统上所有可用的预定义宏。例如:

$ touch dummy.hxx
$ cpp -dM ./dummy.hxx
#define __DBL_MIN_EXP__ (-1021)
#define __FLT_MIN__ 1.17549435e-38F
#define __CHAR_BIT__ 8
#define __WCHAR_MAX__ 2147483647
#define __DBL_DENORM_MIN__ 4.9406564584124654e-324
#define __FLT_EVAL_METHOD__ 0
#define __DBL_MIN_10_EXP__ (-307)
#define __FINITE_MATH_ONLY__ 0
#define __SHRT_MAX__ 32767
#define __LDBL_MAX__ 1.18973149535723176502e+4932L
#define __UINTMAX_TYPE__ long unsigned int
#define __linux 1
#define __unix 1
#define __linux__ 1
...

回答by Viet

I'd be more inclined to test for feature availability than platform name. Try using autoconf.

我更倾向于测试功能可用性而不是平台名称。尝试使用autoconf

Otherwise, this is a comprehensive list of platform defines.

否则,这是平台定义的完整列表

Also check out this pagefor defines regarding compilers, libraries, architectures and devices.

请查看此页面以了解有关编译器、库、架构和设备的定义。

回答by Donal Fellows

Detect OSX with the __APPLE__macro if you must. It's better to use configureto detect features if you can, but not everything works well that way.

__APPLE__如果必须,请使用宏检测 OSX 。configure如果可以的话,最好使用来检测特征,但并非一切都这样。

回答by Paul R

I use __MACH__to test for Mac OS X - it's not 100% unique to Mac OS X (there may still be some old NeXT boxes out there !) but it's good enough for telling the difference between Mac and Linux.

__MACH__用来测试 Mac OS X - 它不是 Mac OS X 100% 独有的(可能仍然有一些旧的 NeXT 盒子!)但它足以区分 Mac 和 Linux 之间的区别。