how-to add “warnings as error” rule to Qt .pro file?

两盒软妹~` 提交于 2019-12-23 07:07:51

问题


When I usually work on a C++ project, one of the first things I do is setting up the "treat warning as errors" on my compiler.

When using Qt, qmake generates the Makefile for you and doesn't include this option on the compilation commands. I'm pretty sure there is a way to add such an option (and others) into the generated Makefile but I couldn't figure it out.

How would I do that ?

I'm using the open-source version of Qt with g++ as the compiler.


回答1:


You can use QMAKE_CXXFLAGS in pro file to specify compiler flags:

QMAKE_CXXFLAGS += -Werror



回答2:


The solution above is for GCC only. For booth compillers (VS and gcc) use:

win32-g++ {
   QMAKE_CXXFLAGS += -Werror
}
win32-msvc*{
   QMAKE_CXXFLAGS += /WX
}



回答3:


There is a QMake variable called QMAKE_CXXFLAGS_WARN_ON which is included into CXXFLAGS whenever CONFIG contains warn_on.

So my project files all include a common.pri which contains:

CONFIG += warn_on

dirty_build: CONFIG += noopt

!dirty_build: WARNINGS += -Werror

# Turn on warnings, except for code that is Qt-generated
WARNINGS += -Wextra
WARNINGS += -Wunknown-pragmas -Wundef
WARNINGS += -Wold-style-cast
WARNINGS += -Wdisabled-optimization -Wstrict-overflow=4
WARNINGS += -Weffc++ -Wuseless-cast
WARNINGS += -Winit-self -Wpointer-arith
WARNINGS += -Wlogical-op
WARNINGS += -Wunsafe-loop-optimizations -Wno-error=unsafe-loop-optimizations
QMAKE_CXXFLAGS_WARN_ON += $(and $(filter-out moc_% qrc_%, $@),$${WARNINGS})

The filter-out exists to disable the warnings for Qt-generated meta-object and resource files.

I also have

# Override the C and C++ targets to selectively replace -I with -isystem for include paths
QMAKE_RUN_CC            = $(CC) -o $obj -c $(CFLAGS) $(subst -I/usr/include,-isystem /usr/include,$(INCPATH)) $src
QMAKE_RUN_CC_IMP        = $(CC) -o $@ -c $(CFLAGS) $(subst -I/usr/include,-isystem /usr/include,$(INCPATH)) $<
QMAKE_RUN_CXX           = $(CXX) -o $obj -c $(CXXFLAGS) $(subst -I/usr/include,-isystem /usr/include,$(INCPATH)) $src
QMAKE_RUN_CXX_IMP       = $(CXX) -o $@ -c $(CXXFLAGS) $(subst -I/usr/include,-isystem /usr/include,$(INCPATH)) $<

That allows me to enable -Weffc++ and others without incurring lots of messages from installed header files.



来源:https://stackoverflow.com/questions/3014947/how-to-add-warnings-as-error-rule-to-qt-pro-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!