When I usually work on a C++
project, one of the first things I do is setting up the "treat开发者_StackOverflow中文版 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.
You can use QMAKE_CXXFLAGS
in pro file to specify compiler flags:
QMAKE_CXXFLAGS += -Werror
The solution above is for GCC only. For both compillers (VS and gcc) use:
win32-g++ {
QMAKE_CXXFLAGS += -Werror
}
win32-msvc*{
QMAKE_CXXFLAGS += /WX
}
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.
精彩评论