Qt – How to include Qt’s headers with -isystem (system headers) with qmake and qt5

g++qmakeqtqt5warnings

I compile my Qt5-based project with warnings enabled on g++:

# project.pro file
QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Wconversion -Weffc++

When compiling, Qt produces lots of warnings (1000+ with just one simple widget), hiding the warnings from my code.

How to tell qmake to use the -isystem switch when specifying the Qt's headers rather than -I to suppress the warnings? I don't want to turn warnings off I want to keep them for my code.

NOTE: I checked this SO question but it does not work in my case, it might be only for Qt4, I use Qt5.

NOTE 2: this is an acknowledged bug, I am looking for a workaround. I use a recent version of qmake compiled from sources 5.4.1, this version passes system headers from /include and /usr/include as system headers but not the Qt's headers.

NOTE 3: I know CMake would work but this is not an option for me.

Best Answer

I found two ways to suppress warnings from Qt's headers, one way by installing Qt in system's path (as suggested in the other answer) and the other directly from your pro file by using GCC flags.

  1. When building your own Qt, configure the header's installation path to one of your system path:

    $ ./configure -headerdir /usr/local/include
    

    System paths are /usr/include or /usr/local/include or one of the rest listed in:

    $ grep DEFAULT_INCDIRS mkspecs/qconfig.pri
    QMAKE_DEFAULT_INCDIRS = /usr/include/c++/4.8 /usr/include/x86_64-linux-gnu/c++/4.8 /usr/include/c++/4.8/backward /usr/lib/gcc/x86_64-linux-gnu/4.8/include /usr/local/include /usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed /usr/include/x86_64-linux-gnu /usr/include
    

    Source: this thread in Qt's devel list.

  2. Or directly in your Qt pro file, simply add the -isystem flag into the QMAKE_CXXFLAGS:

    # the line below suppresses warnings generated by Qt's header files: we tell
    # GCC to treat Qt's headers as "system headers" with the -isystem flag
    QMAKE_CXXFLAGS += -isystem $$[QT_INSTALL_HEADERS]
    

    The resulting GCC command line looks like:

    g++ -c -pipe -isystem /usr/local/Qt-5.4.1/include -Wall ...
        -I/usr/local/Qt-5.4.1/include
        -I/usr/local/Qt-5.4.1/include/QtWidgets
        ...
    

    Note how the Qt's include paths are still added with -I, allowing Qt Creator to "see" all Qt headers, but GCC sees the -isystem flag and suppresses warnings for all subfolders.

Related Topic