21

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.

Community
  • 1
  • 1
Julien-L
  • 5,267
  • 3
  • 34
  • 51
  • The "workaround" is to submit a patch to qmake that adds support for this. Or to switch to cmake, where I hope this is a solved problem. – Kuba hasn't forgotten Monica May 29 '15 at 15:28
  • I have a recent version of qmake (I will edit the question to mention this). I tried with CMake: `include_directories(SYSTEM dir1 dir2)` this works but I need to stick with qmake. Thanks – Julien-L May 29 '15 at 16:09

2 Answers2

16

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.

Julien-L
  • 5,267
  • 3
  • 34
  • 51
  • 1
    No need of that `$$system` -- just use `$$[QT_INSTALL_HEADERS]` :) – peppe May 30 '15 at 08:09
  • I didn't know about the brackets syntax--I tried with just `$$QT_INSTALL_HEADERS` and it did not work, I was missing the brackets! I will update the answer. Thanks! – Julien-L Jun 01 '15 at 21:41
  • 3
    Method 2: If you have several folders inside Qt, you should then add `-isystem $$[QT_INSTALL_HEADERS]/...` for every folder: see http://stackoverflow.com/a/20264397 – John_West Mar 02 '16 at 00:34
  • This works only for gcc-like compilers. Visual Studio's equivalent switch is `/external:I `. https://gitlab.kitware.com/cmake/cmake/issues/17904 – Paulo Carvalho Jan 08 '20 at 17:41
2

Did you install Qt in a system path? Otherwise qmake won't pass -isystem.

You can check which paths are system paths according to qmake by reading your mkspec/qconfig.pri (after you run configure), the system paths are set to the QMAKE_DEFAULT_INCDIRS variable. Here:

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
peppe
  • 21,934
  • 4
  • 55
  • 70