获取编译器警告的摘要

Getting a summary of compiler warnings

有什么方法可以在构建结束时获得所有编译器警告的摘要?在 Makefile

上使用 g++

您可以创建自己的 make 命令进行编译并在您的食谱中使用它。例如,您可以自动将编译器的标准和错误输出记录到文本文件中,并 grep 它们全部用于构建结束时的警告。类似于(使用 GNU make):

# $(1): source file
# $(2): object file
# $(3): log file
MYCXX = $(CXX) $(CXXFLAGS) -c $(1) -o $(2) 2>&1 | tee $(3)

CXXFLAGS += -Wall -pedantic

%.o: %.cpp
    $(call MYCXX,$<,$@,$*.log)

OBJS := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
LOGS := $(patsubst %.o,%.log,$(OBJS))

top: $(OBJS)
    $(CXX) $(LDFLAGS) $^ -o $@
    @printf '\nSummary of warnings\n###################\n\n'
    @for l in $(LOGS); do grep -i -10 warning $$l || true; done

演示:

$ make
g++ -Wall -pedantic -c b.cpp -o b.o 2>&1 | tee b.log
g++ -Wall -pedantic -c c.cpp -o c.o 2>&1 | tee c.log
g++ -Wall -pedantic -c a.cpp -o a.o 2>&1 | tee a.log
g++ -Wall -pedantic -c d.cpp -o d.o 2>&1 | tee d.log
c.cpp: In function ‘int c()’:
c.cpp:1:18: warning: unused variable ‘vc’ [-Wunused-variable]
 int c(void) {int vc; return 0;}
                  ^~
a.cpp: In function ‘int a()’:
a.cpp:1:18: warning: unused variable ‘va’ [-Wunused-variable]
 int a(void) {int va; return 0;}
                  ^~
b.cpp: In function ‘int b()’:
b.cpp:1:18: warning: unused variable ‘vb’ [-Wunused-variable]
 int b(void) {int vb; return 0;}
                  ^~
g++  b.o c.o a.o d.o -o top

Summary of warnings
###################

b.cpp: In function ‘int b()’:
b.cpp:1:18: warning: unused variable ‘vb’ [-Wunused-variable]
 int b(void) {int vb; return 0;}
                  ^~
c.cpp: In function ‘int c()’:
c.cpp:1:18: warning: unused variable ‘vc’ [-Wunused-variable]
 int c(void) {int vc; return 0;}
                  ^~
a.cpp: In function ‘int a()’:
a.cpp:1:18: warning: unused variable ‘va’ [-Wunused-variable]
 int a(void) {int va; return 0;}
                  ^~

甚至还有一种稍微更优雅的方法,包括(重新)定义 CXX 标准 make 变量并为其分配一个小的 shell 脚本来完成这项工作,这样您就可以像往常一样使用 CXX

CXX = function mycxx { g++ $$* 2>&1 | tee $(patsubst %.o,%.log,$@) ;}; mycxx

%.o: %.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

当通过 make 为 foo.o 目标扩展时,配方变为:

function mycxx { g++ $* 2>&1 | tee foo.log ;}; mycxx -Wall -pedantic foo.cpp -o foo.o

而 shell 最终会执行:

g++ -Wall -pedantic foo.cpp -o foo.o 2>&1 | tee foo.log

最终结果应该与第一个解决方案相同,但对 Makefile 的修改要少得多。除了 CXX 的(重新)定义外,它应该保持不变(假设您在食谱中使用 CXX,这是推荐的)。