How to suppress warnings for file included from header

Viewed 2470

I use GCC -Weffc++ option in my Qt project. To suppress warnings from Qt headers i add QMAKE_CXXFLAGS += -isystem $(QTDIR)\include.
But this doesn't suppress all warnings, i still get annoying warnings from QUuid class because $(QTDIR)\include\QtCore\quuid.h
file includes
..\..\src\corelib\plugin\quuid.h.
I tried to add
QMAKE_CXXFLAGS += -isystem $(QTDIR)\src
and
QMAKE_CXXFLAGS += -isystem $(QTDIR)\src\corelib\plugin
but it didn't help. Is there a way to fix this?

2 Answers

You need to suppress each directory separately. Example from my project:

QMAKE_CXXFLAGS += -isystem "$$[QT_INSTALL_HEADERS]/qt5" -isystem "$$[QT_INSTALL_HEADERS]/qt5/QtWidgets" \
                  -isystem "$$[QT_INSTALL_HEADERS]/QtXml" -isystem "/usr/include/qt5/QtGui" \
                  -isystem "$$[QT_INSTALL_HEADERS]/QtCore"

Or, to automate the above for the exact Qt modules you have enabled:

for (inc, QT) {
    QMAKE_CXXFLAGS += -isystem \"$$[QT_INSTALL_HEADERS]/Qt$$system("echo $$inc | sed 's/.*/\u&/'")\"
}

# Still need this separately:
QMAKE_CXXFLAGS += -isystem "$$[QT_INSTALL_HEADERS]/qt5"

This thread helped me a lot, thank you @dismine, but it didn't work for me in some cases, so I'm pasting my solution ( I don't want to edit the first answer )

I have enhanced it to get rid of the sed command, so it is cross-platform, it uses standard qmake's replace functions $$split(), $$join(), $$upper(), and $$take_first().

moduleList local variable is needed, it doesn't work without it.

Following works for Qt >=5.15.2 (it of course can work on lower versions, but I didn't test it):

# Mark all Qt headers as system headers

!win32-clang-msvc:!mingw:!unix: \
    return()

win32-clang-msvc: \
    optionToken = -imsvc
else: \
    optionToken = -isystem

# I don't know why but on MSYS2 this line makes problem, it throws:
# fatal error: stdlib.h: No such file or directory
# On unix it is needed to avoid warnings from system headers
# No warnings on MSYS2 without it
!mingw: \
    QMAKE_CXXFLAGS += $$optionToken $$shell_quote($$[QT_INSTALL_HEADERS])

for(module, QT) {
    equals(module, "testlib"): \
        QMAKE_CXXFLAGS += \
            $$optionToken $$shell_quote($$[QT_INSTALL_HEADERS]/QtTest)
    else {
        # Capitalize a first letter, result: -isystem <path>/include/QtCore
        moduleList = $$split(module, )
        QMAKE_CXXFLAGS += \
            $$optionToken $$shell_quote($$[QT_INSTALL_HEADERS]/Qt$$upper(\
                          $$take_first(moduleList))$$join(moduleList, ))
    }
}

unset(optionToken)
unset(moduleList)

It dynamically generates correct -isystem or -imsvc compiler flags on the base of currently used Qt modules, so you can include this code in different projects/sub-projects.

I can recommend creating the system_headers.prf and enable it by the qmake CONFIG option CONFIG += system_headers.

Original system_headers.prf source code is from the TinyORM project.

Related