gpt4 book ai didi

c++ - 如何从预处理器宏中识别平台/编译器?

转载 作者:IT老高 更新时间:2023-10-28 11:52:06 27 4
gpt4 key购买 nike

我正在编写一个跨平台代码,它应该在 linux、windows、Mac OS 上编译。在windows上,我必须支持visual studio和mingw。

有一些特定于平台的代码,我应该将它们放在 #ifdef .. #endif 环境中。比如这里我放了win32的具体代码:

#ifdef WIN32
#include <windows.h>
#endif

但是我如何识别 linux 和 mac OS?我应该使用什么定义名称(或等)?

最佳答案

对于Mac OS:

#ifdef __APPLE__

对于 Windows 上的 MingW:

#ifdef __MINGW32__

对于 Linux:

#ifdef __linux__

对于其他 Windows 编译器,请查看 this threadthis对于其他几个编译器和架构。

关于c++ - 如何从预处理器宏中识别平台/编译器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4605842/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com