Tag Archives: boost

qmake: Adding Boost C++ libraries’ include directory

During cross-platform development using Qt you might want to use Boost C++ libraries.
It can be done simply by adding the line to the .pro file:

INCLUDEPATH += /path/to/boost_root

But it’s not right way. On another machine Boost C++ libraries may be installed into another directory.
Furthermore, if there is a SubDirs Project and all the subprojects use Boost C++ libraries, the INCLUDEPATH statement must be added into every subproject’s .pro file.

To simplify this I have done it this way.
First of all, it would be quite convenient to use either qmake variable (for example, run qmake BOOST_ROOT=/path/to/boost_root) or system environment variable (export BOOST_ROOT=/path/to/boost_root) to find Boost C++ libraries.
Here is how it looks like:

# Find Boost library.

# Try to use qmake variable's value.
_BOOST_ROOT = $$BOOST_ROOT
isEmpty(_BOOST_ROOT) {
    message(\"Boost Library\" qmake value not detected...)

    # Try to use the system environment value.
    _BOOST_ROOT = $$(BOOST_ROOT)
}

isEmpty(_BOOST_ROOT) {
    message(\"Boost Library\" environment variable not detected...)
    !build_pass:error(Please set the environment variable `BOOST_ROOT`. For example, BOOST_ROOT=c:\\boost_1_53_0)
} else {
    message(\"Boost Library\" detected in BOOST_ROOT = \"$$_BOOST_ROOT\")
    INCLUDEPATH += $$_BOOST_ROOT
}

The BOOST_ROOT qmake variable’s value is checked.
If empty, BOOST_ROOT system variable’s value is checked.

Second, to re-use this code without duplication, extract the code into common.pri file in the SubDirs’ project directory. Then, add the include () statement into subprojects .pro files, for example:

include (../common.pri)

Enjoy!