在 C++ 运行时获取当前操作系统
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3063110/
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
Get the current operating system during runtime in C++
提问by Kai Walz
I need to figure out the operating system my program is running on during runtime.
我需要弄清楚我的程序在运行时运行的操作系统。
I'm using Qt 4.6.2, MinGW and Eclipse with CDT. My program shall run a command-line QProcess on Windows or Linux. Now I need a kind of switch to run the different code depending on the operating system.
我将 Qt 4.6.2、MinGW 和 Eclipse 与 CDT 一起使用。我的程序将在 Windows 或 Linux 上运行命令行 QProcess。现在我需要一种开关来根据操作系统运行不同的代码。
Thx
谢谢
采纳答案by Alexis Wilke
Actually the Operating System is defined by the Q_OS_... macros. Just saying. The Q_WS_... are windowing system. Not exactly the same. (I'm just reading what the author of the question wrote.... "operating system".)
实际上操作系统是由 Q_OS_... 宏定义的。就是说。Q_WS_... 是窗口系统。不完全一样。(我只是在阅读问题作者所写的内容......“操作系统”。)
These declarations are found in the qglobal.h file.
这些声明可以在 qglobal.h 文件中找到。
Use Q_OS_x with x being one of:
DARWIN - Darwin OS (synonym for Q_OS_MAC)
SYMBIAN - Symbian
MSDOS - MS-DOS and Windows
OS2 - OS/2
OS2EMX - XFree86 on OS/2 (not PM)
WIN32 - Win32 (Windows 2000/XP/Vista/7 and Windows Server 2003/2008)
WINCE - WinCE (Windows CE 5.0)
CYGWIN - Cygwin
SOLARIS - Sun Solaris
HPUX - HP-UX
ULTRIX - DEC Ultrix
LINUX - Linux
FREEBSD - FreeBSD
NETBSD - NetBSD
OPENBSD - OpenBSD
BSDI - BSD/OS
IRIX - SGI Irix
OSF - HP Tru64 UNIX
SCO - SCO OpenServer 5
UNIXWARE - UnixWare 7, Open UNIX 8
AIX - AIX
HURD - GNU Hurd
DGUX - DG/UX
RELIANT - Reliant UNIX
DYNIX - DYNIX/ptx
QNX - QNX
QNX6 - QNX RTP 6.1
LYNX - LynxOS
BSD4 - Any BSD 4.4 system
UNIX - Any UNIX BSD/SYSV system
The window system definitions are like this:
窗口系统定义如下:
Use Q_WS_x where x is one of:
MACX - Mac OS X
MAC9 - Mac OS 9
QWS - Qt for Embedded Linux
WIN32 - Windows
X11 - X Window System
S60 - Symbian S60
PM - unsupported
WIN16 - unsupported
One of the main problems with using #ifdef is to make sure that if you compile on a "new" platform (never compiled that software on that platform) then you want to use #elif defined(...)
and at least an #else
+ #error
...
使用#ifdef 的主要问题之一是确保如果您在“新”平台上编译(从未在该平台上编译该软件),那么您想要使用#elif defined(...)
并且至少有一个#else
+ #error
...
#ifdef W_OS_LINUX
std::cout << "Linux version";
#elif defined(W_OS_CYGWIN)
std::cout << "Cygwin version";
#else
#error "We don't support that version yet..."
#endif
回答by Martin Beckett
In Qt the following OS macros are defined for compile time options
在 Qt 中,为编译时选项定义了以下 OS 宏
// pre Qt5
Qt/X11 = Q_WS_X11 is defined.
Qt/Windows = Q_WS_WIN is defined.
Qt/Mac OS X = Q_WS_MACX is defined
// 预 Qt5 Qt/X11 = Q_WS_X11 已定义。
Qt/Windows = Q_WS_WIN 已定义。
Qt/Mac OS X = Q_WS_MACX 已定义
// For Qt5 onwards
Qt/X11 = Q_OS_X11 is defined.
Qt/Windows = Q_OS_WIN is defined.
Qt/Mac OS X = Q_OS_MACX is defined
// 对于 Qt5 以后的版本,定义了 Qt/X11 = Q_OS_X11。
Qt/Windows = Q_OS_WIN 已定义。
Qt/Mac OS X = Q_OS_MACX 已定义
Then the QSysInfoclass gives you the OS version and other options at runtime.
然后QSysInfo类在运行时为您提供操作系统版本和其他选项。
回答by Troubadour
回答by Vitaly Inflyanskas
Since Qt5 macroses Q_WS_* are not defined!
由于 Qt5 宏 Q_WS_* 没有定义!
You should use Q_OS_* macroses instead:
您应该改用 Q_OS_* 宏:
Q_OS_AIX
Q_OS_ANDROID
Q_OS_BSD4
Q_OS_BSDI
Q_OS_CYGWIN
Q_OS_DARWIN - Darwin-based OS such as OS X and iOS, including any open source version(s) of Darwin.
Q_OS_DGUX
Q_OS_DYNIX
Q_OS_FREEBSD
Q_OS_HPUX
Q_OS_HURD
Q_OS_IOS
Q_OS_IRIX
Q_OS_LINUX
Q_OS_LYNX
Q_OS_MAC - Darwin-based OS distributed by Apple, which currently includes OS X and iOS, but not the open source version.
Q_OS_NETBSD
Q_OS_OPENBSD
Q_OS_OSF
Q_OS_OSX
Q_OS_QNX
Q_OS_RELIANT
Q_OS_SCO
Q_OS_SOLARIS
Q_OS_ULTRIX
Q_OS_UNIX
Q_OS_UNIXWARE
Q_OS_WIN32 - 32-bit and 64-bit versions of Windows (not on Windows CE).
Q_OS_WIN64
Q_OS_WIN - all supported versions of Windows. That is, if Q_OS_WIN32, Q_OS_WIN64 or Q_OS_WINCE is defined.
Q_OS_WINCE
Q_OS_WINPHONE
Q_OS_WINRT
More details in documentation of QtGlobal
QtGlobal文档中的更多详细信息
回答by Neurotransmitter
Most of these answers provide solutions for determining the required info at a compiletime, when your app is being compiled on yourdevelopment machine.
当您的应用程序在您的开发机器上编译时,这些答案中的大多数提供了在编译时确定所需信息的解决方案。
Here is a how you get the required info during runtime, when your app is run by users of your app on theirmachines.
当您的应用程序的用户在他们的机器上运行您的应用程序时,这是在运行时获取所需信息的方式。
qDebug() << "currentCpuArchitecture():" << QSysInfo::currentCpuArchitecture();
qDebug() << "productType():" << QSysInfo::productType();
qDebug() << "productVersion():" << QSysInfo::productVersion();
qDebug() << "prettyProductName():" << QSysInfo::prettyProductName();
Typical result:
典型结果:
21:43:09.855 Debug: currentCpuArchitecture(): "x86_64"
21:43:09.855 Debug: productType(): "windows"
21:43:09.855 Debug: productVersion(): "10"
21:43:09.855 Debug: prettyProductName(): "Windows 10 (10.0)"
Documentation for QSysInfo
is here.
对于文档QSysInfo
是在这里。
回答by houbysoft
Do it at compile time using #ifdef.
在编译时使用#ifdef 执行此操作。
Under windows, WIN32 is defined.
在windows下,定义了WIN32。
So, do:
所以,做:
#ifdef WIN32
// Windows code here
#else
// UNIX code here
#endif
回答by kaegoorn48
for runtime QGuiApplication::platformName()
对于运行时QGuiApplication::platformName()
This more accurately distinguish, for example, "eglfs" or "directfb"
这更准确地区分,例如“eglfs”或“directfb”
回答by infixed
Starting with Qt 5.9, some methods to QSysInfo have been depreciated such as QSysInfo::windowsVersion()
从 Qt 5.9 开始,QSysInfo 的一些方法已被弃用,例如 QSysInfo::windowsVersion()
An alternative class for such functionality starting in Qt 5.9 is QOperatingSystemVersion
从 Qt 5.9 开始,此类功能的替代类是 QOperatingSystemVersion
example
例子
bool onWindows = ( QOperatingSystemVersion::Windows == QOperatingSystemVersion::currentType() );
回答by Alex
This is typically done using precompiler directives to control what chunk of code is included/excluded from your build.
这通常是使用预编译器指令来完成的,以控制构建中包含/排除的代码块。
#ifdef WIN32
// ...
#endif
This results in (arguably) uglier code, but targeted binaries.
这导致(可以说)更丑陋的代码,但有针对性的二进制文件。