1

I need to figure out the operating system my program is running on during runtime.

I'm using Qt 4.8.1, MinGW. 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. I'm aware of macros like Q_OS_WIN23, Q_OS_LINUX and etc. But I don't know how to switch the part of code while compiling it, so that depending on OS it ignored irrelevant one? Could you show via snippet? Thanks beforehand!

elgolondrino
  • 665
  • 9
  • 33

2 Answers2

5

Use #ifdefs in order to figure out in compilation time the operating system:

#ifdef Q_OS_MAC
// mac code here
#endif

#ifdef Q_OS_LINUX
// linux specific code here
#endif

#ifdef Q_OS_WIN32
// windows code here
#endif
pnezis
  • 12,023
  • 2
  • 38
  • 38
1

I prefer to do it on project level and separate platform specific code in each file. See documentation.

Example:

win32 {
    SOURCES += paintwidget_win.cpp
}
Marek R
  • 32,568
  • 6
  • 55
  • 140