如何检查当前操作系统是 Windows、Linux 还是 OSX?

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

How to check if the current operating system is Windows, Linux, or OSX?

windowslinuxmacosporting

提问by Kizaru

I'm writing a compiler project which will produce assembly code as the target language. However, there are some small changes that need to be accounted for depending on the operating system, and I'm not sure how to check for the OS. In case it matters, I am concerned with only 32bit. I've seen in some source code something like

我正在编写一个编译器项目,它将生成汇编代码作为目标语言。但是,根据操作系统的不同,需要考虑一些小的更改,我不确定如何检查操作系统。如果重要,我只关心 32 位。我在一些源代码中看到过类似的东西

#ifdef WIN32

but I have no idea how/if this works.

但我不知道这如何/是否有效。

EDIT: Some clarification. I am using gcc on all three platforms. I don't know if macros like WIN32 are defined via gcc in each platform. If so, these constants seem to solve my issue.

编辑:一些澄清。我在所有三个平台上都使用 gcc。不知道WIN32之类的宏是不是每个平台都是通过gcc定义的。如果是这样,这些常量似乎解决了我的问题。

回答by Greg Hewgill

The use of #ifdefuses preprocessor symbols to conditionally compile code depending on which symbols are defined. In your example, the compiler or an #includefile may define WIN32, which means the code is being compiled in a Win32 environment.

使用#ifdef有条件地编译代码使用预处理器符号,这取决于符号的定义。在您的示例中,编译器或#include文件可能会定义WIN32,这意味着代码是在 Win32 环境中编译的。

Depending on the compiler and the platform, there may be different predefined symbols that indicate the operating system or processor architecture (among many other possible things) that relate to the current compilation environment.

根据编译器和平台的不同,可能会有不同的预定义符号来指示与当前编译环境相关的操作系统或处理器架构(以及许多其他可能的事物)。

With gcc, you can show the predefined symbols using the following command line:

使用gcc,您可以使用以下命令行显示预定义的符号:

gcc -E -dM - </dev/null

On my machine, one of the defined symbols is:

在我的机器上,定义的符号之一是:

#define __FreeBSD__ 8

This happens to mean that I'm running FreeBSD version 8.

这恰好意味着我正在运行 FreeBSD 版本 8。

(The above applies to C and C-family languages such as C++.)

(以上适用于 C 和 C 系列语言,如 C++。)

回答by Andy Dent

Generally the essential platform-determinign predefined macros are defined by the compiler itself, often depending on other switches.

通常,基本的平台确定预定义宏是由编译器本身定义的,通常取决于其他开关。

Best way for a given compiler is to search for "predefined macros" or "predefined symbols" in their help. eg: googling visual studio predefined macrosyields this page.

给定编译器的最佳方法是在其帮助中搜索“预定义宏”或“预定义符号”。例如:谷歌搜索Visual Studio 预定义的宏产生这个页面

My OOFILE frameworks covered a wide range of systems. The oofplat.h is available from Sourceforge svn browserand unifies the different compiler definitions into _Windows etc.

我的 OOFILE 框架涵盖了广泛的系统。oofplat.h 可从Sourceforge svn 浏览器获得,并将不同的编译器定义统一到 _Windows 等中。

Relevant portions copied below. Note that it's fairly ignorant when it comes to differentiating Unix versions as there weren't built-ins defined by the compiler.

相关部分复制如下。请注意,在区分 Unix 版本时它是相当无知的,因为编译器没有定义内置函数。

#ifndef _Windows
 #if defined(_WIN32)
  #define _Win32
  #define _Windows
 #elif defined(WIN32)
  #define _Win32
  #define _Windows
 #elif defined(__WIN32__)
  #define _Win32
  #define _Windows
 #elif defined(__Win32__)
  #define _Win32
  #define _Windows
 #elif defined(_WINDOWS)
  #define _Windows
 #elif defined(__INTEL__) && defined(__MWERKS__)
 // Metrowerks CodeWarrior doesn't build anything other than Win32 on INTEL, no DOS
  #define _Windows
  #define _Win32
 #endif
#else
 #if defined __Win32__ || defined _WIN32
  #ifndef _Win32
   #define _Win32
  #endif
 #endif
#endif

#ifndef _MSDOS
 #ifdef _Windows
  #define _MSDOS
 #elif defined(MSDOS)
  #define _MSDOS
 #elif defined(__MSDOS__)
  #define _MSDOS
 #endif
#endif

#ifdef _Windows
 #ifndef STRICT
// some Windows headers define STRICT. In Visual C++ at least having it defined
// affects how static member signatures are mangled, so we define it up front  
  #define STRICT
 #endif
#endif


// if not a DOS machine by now, may be Mac or Unix
// cope with Metrowerks and Symantec (and MPW?)
#ifndef _MSDOS
 #ifndef _Macintosh
  #ifdef macintosh
   #define _Macintosh
  #endif
 #endif
 #ifndef _Macintosh
  #define _Unix
 #endif
#endif

回答by Adonais

This is the power of the C preprocessors. First at all, you can define macros:

这就是 C 预处理器的强大之处。首先,您可以定义宏:

#define MY_MACRO

After this, you can check if a macro is defined:

在此之后,您可以检查是否定义了宏:

#ifdef MY_MACRO
code, code, code
code, code, code
#endif

That means the code inside #ifdefand #endifblocks will be valid only if that macro was defined. Else it'll be ignored, and with ignoredI mean it'll be like you haven't never written it.

这意味着只有在定义了该宏时#ifdef#endif块中的代码才有效。否则它会被忽略,而忽略我的意思是它就像你从未写过它一样。

The opposite of #ifdefis #ifndef. #endifis the same for both cases.

的反义词#ifdef#ifndef#endif两种情况相同。

In your case, you can use macros in order to change what a function will do:

在您的情况下,您可以使用宏来更改函数的作用:

#define MY_MACRO

void my_function() {
#ifdef MY_MACRO
    code_for_my_macro();
#endif
#ifdef ANOTHER_MACRO
    code_for_another_macro();
#endif
}

..so the only thing you'll need to do to port your code is changing the macro.

..所以移植代码所需要做的唯一一件事就是更改宏。

Macros have more utilities. Search for "C preprocessors" and you'll see all what you can do with them.

宏有更多的实用程序。搜索“C 预处理器”,您将看到可以使用它们执行的所有操作。