如何在C ++中运行时检测当前操作系统? [英] How to detect current operating system at runtime in c++?

查看:54
本文介绍了如何在C ++中运行时检测当前操作系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测我的.exe在哪个操作系统上运行,以便我可以根据基础操作系统执行特定的操作.我希望它仅适用于Windows和Mac.

I want to detect which operating system my .exe is being run on, so that I could perform specific operations depending on the underlying operating system. I want it to be just for windows and mac.

推荐答案

我想检测我的.exe在哪个操作系统上运行

I want to detect which operating system my .exe is being run on

(请注意,在MacOSX上,可执行文件通常不带有 .exe 后缀;该约定仅适用于Windows)

(notice that on MacOSX executable files are traditionally not suffixed with .exe; that convention is specific to Windows)

您无法在运行时在标准C ++ 11中检测到这一点(因此,从技术上讲,这没有任何意义),因为C ++ 11标准 n3337 不了解操作系统(有些可以为裸机编译C ++源代码.我建议条件编译(例如 #if )并使用特定于操作系统的标头(并配置您的 boost Qt ....)可能会有所帮助,但您需要确定选择一种(他们可能会尝试在几种操作系统之上提供一些常见的抽象,但是邪恶仍然存在于细节中,例如Windows和MacOSX上的文件路径仍然不同.

You cannot detect that in pure standard C++11 at runtime (so your question makes technically no sense), because the C++11 standard n3337 does not know about operating systems (some C++ source code may be compiled for the bare metal). I would recommend conditional compilation (e.g. #if) and using operating system specific headers (and configuring your build automation to detect them). Some C++ frameworks (boost, poco, Qt....) could be helpful, but you need to decide to choose one (they might try to provide some common abstractions above several OSes, but the evil is still in the details, e.g. file paths are still different on Windows and on MacOSX).

MicroSoft Windows 有自己的 WinAPI (实际上仅由Windows使用.可能有一天 ReactOS 会实现大多数API),请此处. MacOSX 或多或少地符合

MicroSoft Windows has its own API, called WinAPI (which is only, in practice, used by Windows. Perhaps some day ReactOS would implement most of that API), documented here. MacOSX is more or less conforming to POSIX, which is a standard API about a family of OSes (but Windows don't care much about that standard).

如果您想全面了解操作系统(明智的做法),请阅读例如

If you want to learn more about OSes in general (a sensible thing to do), read for example Operating Systems: Three Easy Pieces (freely downloadable textbook).

实际上,从C ++源代码编译的可执行文件文件特定于操作系统它旨在运行,其文件格式也特定某些操作系统(例如 PE 在Linux上为Executable_and_Linkable_Format"rel =" nofollow noreferrer> ELF , Mach-O 在MacOSX上...).因此,Windows可执行文件将无法在MacOSX上运行,反之亦然(胖二进制的想法已经出来了2018年的时尚).您可能会调整您的构建过程(例如,您的忍者等).该构建过程可能(而且经常)传递额外的编译标志(例如,某些 -DBUILD_FOR_MACOSX 编译器您的预处理器的标志,如果是为MacOSX构建的;那么您可能会在少数地方在C ++源代码中使用某些预处理器指令,例如 #if BUILD_FOR_MACOSX ) 特定于目标操作系统.您甚至可以自定义构建,以便在MacOSX上编译某些 for-macosx.cc C ++文件(及其链接到的目标文件),但是在Windows上,您将使用其他一些for_windows.cc C ++文件.有时,构建过程足够灵活,可以在构建时自动检测什么操作系统(但是如何执行此操作是另一个问题.GNU autoconf 可能很有启发性.)

In practice, an executable file compiled from C++ source code is specific to the operating system it is designed to run on, and its file format is also specific to some OS (e.g. PE on Windows, ELF on Linux, Mach-O on MacOSX...). So a Windows executable won't run on MacOSX and vice versa (the fat binary idea is out of fashion in 2018). You probably would adjust your build procedure (e.g. your Makefile, or with cmake, ninja, etc...) for your OS. That build procedure might (and often does) pass extra compilation flags (e.g. some -DBUILD_FOR_MACOSX compiler flag for your preprocessor, if building for MacOSX; then you might -in few places- use some preprocessor directive like #if BUILD_FOR_MACOSX in your C++ source code) specific to the target operating system. You could even customize your build so that on MacOSX some for-macosx.cc C++ file is compiled (and its object file linked into) your software, but on Windows you'll use some other for_windows.cc C++ file. Sometimes, the build procedure is flexible enough to auto-detect at build time what is the operating system (but how to do that is a different question. The GNU autoconf could be inspirational).

一旦为您的操作系统配置了构建过程(或已自动检测到),则可以使用预处理器条件工具 #include 适当的系统特定头,并编译对操作系统特定功能的调用(例如POSIX

Once your build procedure has been configured for (or has auto-detected) your operating system, you could use preprocessor conditional facilities to #include appropriate system-specific headers and to compile calls to OS specific functions (such as POSIX uname) querying further at runtime about the OS.

某些编译器预定义了一组预处理器符号,而这些符号取决于目标操作系统和处理器以及您的编译器.如果您使用 GCC ,则可能会创建一些 empty 文件 empty.cc 并使用 g ++ -c -C -E -dM (也许还使用其他相关标志,例如 -std = c ++ 11 )将其编译为找出在您的情况下预定义了哪些预处理器符号.

Some compilers predefine a set of preprocessor symbols, and that set depends upon the target operating system and processor and of your compiler. If you use GCC, you might create some empty file empty.cc and compile it with g++ -c -C -E -dM (perhaps also with other relevant flags, e.g. -std=c++11) to find out which set of preprocessor symbols are predefined in your case.

考虑研究某些跨平台免费软件的源代码(包括构建过程!)项目(例如在 github 或其他地方)上寻求灵感.您还可以找到许多有关多平台C ++构建的资源(例如一个和很多个.)

Consider studying the source code (including the build procedure!) of some cross-platform free software projects (e.g. on github or somewhere else) for inspiration. You can also find many resources about multi-platform C++ builds (e.g. this one and many others).

这篇关于如何在C ++中运行时检测当前操作系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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