Get the current operating system during runtime in C++

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.


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