如何使用预处理器指令检查操作系统? [英] How do I check OS with a preprocessor directive?

查看:22
本文介绍了如何使用预处理器指令检查操作系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要我的代码根据编译它的操作系统来做不同的事情.我正在寻找这样的东西:

I need my code to do different things based on the operating system on which it gets compiled. I'm looking for something like this:

#ifdef OSisWindows
// do Windows-specific stuff
#else
// do Unix-specific stuff
#endif

有没有办法做到这一点?有没有更好的方法来做同样的事情?

Is there a way to do this? Is there a better way to do the same thing?

推荐答案

预定义OS 的宏 站点有一个非常完整的检查列表.以下是其中一些,以及指向它们所在位置的链接:

The Predefined Macros for OS site has a very complete list of checks. Here are a few of them, with links to where they're found:

_WIN32 32 位和 64 位
_WIN64 仅限 64 位
__CYGWIN__

_WIN32   Both 32 bit and 64 bit
_WIN64   64 bit only
__CYGWIN__

有关使用此检查的一些陷阱,请参阅此相关问题.

See this related question on some of the pitfalls of using this check.

unix
__unix
__unix__

__APPLE__ 也用于经典
__MACH__

两者都已定义;检查两者都应该有效.

Both are defined; checking for either should work.

__linux__
linux 过时(不符合 POSIX)
__linux 过时(不符合 POSIX)

__linux__
linux Obsolete (not POSIX compliant)
__linux Obsolete (not POSIX compliant)

__FreeBSD__

__ANDROID__

这篇关于如何使用预处理器指令检查操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆