Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Operating System during compile time

I am new to QT and I am struggling to understand how to determine the operating system before the main function is executed. I am a complete newbie in this area so any guidance would be greatly appreciated.

I would like to determine if the program is running in one of the following environments:

Windows
Unix
Linux
Mac

Also how would I use this data in the main function?

Many thanks

like image 696
NSPredator Avatar asked Dec 08 '15 20:12

NSPredator


3 Answers

You can use preprocessor definitions to work out what platform the code is being compiled on.

For example, to check if you're on Windows:

#if (defined (_WIN32) || defined (_WIN64))
    // windows code
#endif

For Linux:

#if (defined (LINUX) || defined (__linux__))
    // linux code
#endif

...and so on for each platform you're interested in. This way, code not relevant to the platform you're targeting will be removed.

Here's an example of some code that uses this:

#include <iostream>

int main()
{
    #if (defined (_WIN32) || defined (_WIN64))
        std::cout << "I'm on Windows!" << std::endl;
    #elif (defined (LINUX) || defined (__linux__))
        std::cout << "I'm on Linux!" << std::endl;
    #endif
}
like image 192
OMGtechy Avatar answered Sep 21 '22 21:09

OMGtechy


Qt itself defines preprocessor-switches for the different platforms Qt supports. They all look like Q_OS_* - the *-part gets replaced by the different operating system. For you example, that would be:

  • Windows: Q_OS_WIN, Q_OS_WIN32 or Q_OS_WIN64 (there are a few more, Q_OS_WINCE for example, but Q_OS_WIN is defined for all windows-like os)
  • Unix: Q_OS_UNIX
  • Linux: Q_OS_LINUX (Please note that Q_OS_UNIX is defined on linux too)
  • Mac: Q_OS_MAC(for OsX and iOs), Q_OS_OSX or Q_OS_MACX

All those plattform-headers are define in qsystemdetection.h. This file includes a list of almost all of the different supported operating systems and their preprocessor-switch.

like image 44
Felix Avatar answered Sep 18 '22 21:09

Felix


It is nicely documented in QtGlobals.

Q_OS_AIX
Q_OS_ANDROID
Q_OS_BSD4
Q_OS_BSDI
Q_OS_CYGWIN
Q_OS_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
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
Q_OS_WIN64
Q_OS_WIN
Q_OS_WINCE
Q_OS_WINPHONE
Q_OS_WINRT
like image 34
Marek R Avatar answered Sep 21 '22 21:09

Marek R