Reputation: 796
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.
Upvotes: 30
Views: 45034
Reputation: 20818
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".)
These declarations are found in the qglobal.h file.
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 Q_OS_LINUX
std::cout << "Linux version";
#elif defined(Q_OS_CYGWIN)
std::cout << "Cygwin version";
#else
#error "We don't support that version yet..."
#endif
Upvotes: 28
Reputation: 1155
Starting with Qt 5.9, some methods to QSysInfo have been depreciated such as QSysInfo::windowsVersion()
An alternative class for such functionality starting in Qt 5.9 is QOperatingSystemVersion
example
bool onWindows = ( QOperatingSystemVersion::Windows == QOperatingSystemVersion::currentType() );
Upvotes: 4
Reputation: 129
for runtime QGuiApplication::platformName()
This more accurately distinguish, for example, "eglfs" or "directfb"
Upvotes: 3
Reputation: 6827
Most of these answers provide solutions for determining the required info at a compile time, when your app is being compiled on your development machine.
Here is a how you get the required info during runtime, when your app is run by users of your app on their machines.
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.
Upvotes: 17
Reputation: 96177
In Qt the following OS macros are defined for compile time options
// 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
// 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
Then the QSysInfo class gives you the OS version and other options at runtime.
Upvotes: 24
Reputation: 123
Since Qt5 macroses Q_WS_* are not defined!
You should use Q_OS_* macroses instead:
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
Upvotes: 9
Reputation: 13431
Qt offers QSysInfo if you really need to get at this at run-time. Useful for appending to a crash report but for anything else try to do it at compile time.
Upvotes: 15
Reputation: 3652
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.
Upvotes: 1
Reputation: 33430
Do it at compile time using #ifdef.
Under windows, WIN32 is defined.
So, do:
#ifdef WIN32
// Windows code here
#else
// UNIX code here
#endif
Upvotes: 4