Defining the operating system at compile time

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

I would like to determine if a program works in one of the following environments:

Windows
Unix
Linux
Mac

Also, how will I use this data in the main function?

Many thanks

+4
source share
5 answers

You can use preprocessor definitions to determine which platform compiles the code.

For example, to check if you are on Windows:

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

For Linux:

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

... .. . , , , .

, :

#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
}
+5

Qt - Qt. Q_OS_* - * -part . , :

  • Windows: Q_OS_WIN, Q_OS_WIN32 Q_OS_WIN64 (, , Q_OS_WINCE, Q_OS_WIN os)
  • Unix: Q_OS_UNIX
  • Linux: Q_OS_LINUX ( , Q_OS_UNIX Linux)
  • Mac: Q_OS_MAC ( OsX iOs), Q_OS_OSX Q_OS_MACX

qsystemdetection.h. -.

+3

-DOPERATING_SYSTEM=<...> .

enum OperatingSytem {OS_WINDOWS, OS_UNIX, OS_LINUX, OS_MAX};
OperatingSystem os = OPERATING_SYSTEM;

, OPERATING_SYSTEM .

+1

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
+1

, , :

#include <QtGlobal>
#include <QDebug>

enum OperatingSytem {OS_WINDOWS, OS_UNIX, OS_LINUX, OS_MAC};

#if (defined (Q_OS_WIN) || defined (Q_OS_WIN32) || defined (Q_OS_WIN64))
    OperatingSystem os = OS_WINDOWS;
#elif (defined (Q_OS_UNIX))
    OperatingSystem os = OS_UNIX;
#elif (defined (Q_OS_LINUX))
    OperatingSystem os = OS_LINUX;
#elif (defined (Q_OS_MAC))
    OperatingSystem os = OS_MAC;
#endif

int main() {

    switch(os) {
         case OS_WINDOWS: qDebug() << "Windows"; break;
         case OS_UNIX   : qDebug() << "Unix"; break;
         case OS_LINUX  : qDebug() << "Linux"; break;
         case OS_MAC    : qDebug() << "Mac"; break;
         default        : qDebug() << "Unknown"; break;
    }

} 

, Unix linux, , , , , Unix vs Linux.

+1

Source: https://habr.com/ru/post/1619105/


All Articles