Kai Walz Kai Walz - 1 year ago 123
C++ Question

Get the current operating system during runtime in C++

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.


Answer Source

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
 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 W_OS_LINUX
  std::cout << "Linux version";
#elif defined(W_OS_CYGWIN)
  std::cout << "Cygwin version";
#error "We don't support that version yet..."
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download