How to Configure a Single Qt Project for Different Operating Systems

Posted on Posted in Qt Tutorials, Tutorials

There are many cases in every cross-platform development framework that you want your source code to be built differently under different operating systems. For example you might want a method to to use platform specific functions in Windows, Linux and Mac OS X but you don’t want to have different projects for each one of them. If you are in a similar situation then you can use the following approach to have a single integrated project for all operating systems that you are developing for.

First comes the .PRO file configuration. Qt uses qmake for configuring and making its projects and you need to use something like the following in order to have different entries and definitions in your project for different operating systems.

win32 {
#enter your Windows definitions here …
}

unix {
macx {
# Mac OS X definitions should be entered here …
} else {
# Linux definitions go here …
}
}

Normally one can design the project structure in a way that it is sufficient to have a cross-platform project only by adding the template above to your code (PRO file) but it’s possible that sometimes you will need to specify the operating system inside your CPP files (C++ code). In this case you have to use #ifdef or #ifndef directives as seen below:

For Windows you should use the following:

#ifdef Q_OS_WIN

// Code that will only be compiled under Windows

#endif

For Mac OS X you should use the following:

#ifdef Q_OS_MACX

// Code that will only be compiled under MAC OS X

#endif

For Linux you should use the following:

#ifdef LINUX

// Code that will only be compiled under Linux

#endif

Leave a Reply

Your email address will not be published. Required fields are marked *