Compiling googletest for gcov

I want to get coverage information from my googletest tests, but it's hard for me to find good instructions.

I assume that I should compile my gtest binary so that it will .gcno and .gcna . However, no combination of compiler flags seems to happen.

I tried using --coverage and -fprofile-arcs -ftest-coverage for both compilation and linking, but to no avail.

Am I just mistaken in the whole approach? Will googletest tests ever compile like this?

For completeness, the entire make file is presented here:

 # -*- indent-tabs-mode:t; -*- ## Vars CXX=g++ BDDOBJ=../obj OBDD_DIR=../src OBDD_INCLUDE=-I$(OBDD_DIR) #FLAGS=-Wfatal-errors -I./gtest-1.7.0/include -L./gtest-1.7.0/lib/.libs -lgtest -lgtest_main -lpthread GTEST_DIR=./gtest-1.7.0 SRCDIR=./src OBJDIR=./obj BINDIR=./bin LIBDIR=./lib VPATH=$(SRCDIR) $(OBJDIR) $(LIBDIR) $(BINDIR) COVERAGE=-O0 -g --coverage MKDIR=mkdir -p RM=rm -rf .PHONY: clean .INTERMEDIATE: default $(BINDIR) $(OBJDIR) $(LIBDIR) default: obddtest #Binary obddtest: $(BINDIR) libgtest.a Vertex.o Vertex_unittest.o Edge.o Edge_unittest.o Graph.o Graph_unittest.o main.o g++ $(COVERAGE) -isystem ${GTEST_DIR}/include $(OBDD_INCLUDE) -pthread \ $(OBJDIR)/Vertex.o \ $(OBJDIR)/Vertex_unittest.o \ $(OBJDIR)/Graph_unittest.o \ $(OBJDIR)/Edge.o \ $(OBJDIR)/Edge_unittest.o \ $(OBJDIR)/Graph.o \ $(OBJDIR)/main.o \ $(LIBDIR)/libgtest.a \ -o $(BINDIR)/obddtest ## Main main.o: main.cc g++ $(COVERAGE) -isystem ${GTEST_DIR}/include $(OBDD_INCLUDE) -pthread -c $< -o $(OBJDIR)/$@ ## gtest library gtest-all.o: $(OBJDIR) g++ $(COVERAGE) -isystem ${GTEST_DIR}/include -I${GTEST_DIR} -pthread -c ${GTEST_DIR}/src/gtest-all.cc -o $(OBJDIR)/gtest-all.o libgtest.a: $(LIBDIR) gtest-all.o ar -rv $(LIBDIR)/libgtest.a $(OBJDIR)/gtest-all.o ## Source under test %.o: $(OBDD_DIR)/%.cpp $(OBJDIR) g++ $(COVERAGE) -fPIC -O0 $(OBDD_INCLUDE) -c $< -o $(OBJDIR)/$@ ## Tests %_unittest.o: %_unittest.cc $(OBJDIR) g++ $(COVERAGE) -isystem ${GTEST_DIR}/include $(OBDD_INCLUDE) -pthread -DTESTDATA=\"$(CURDIR)/data/\" -c $< -o $(OBJDIR)/$@ ## Housekeeping $(LIBDIR): $(MKDIR) $(LIBDIR) $(OBJDIR): $(MKDIR) $(OBJDIR) $(BINDIR): $(MKDIR) $(BINDIR) clean: $(RM) $(LIBDIR) $(OBJDIR) $(BINDIR) 
+8
c ++ gcc googletest gcov
source share
1 answer

You must add --coverage both for compilation and for compilation, as you already do right. This generates a bytecode that will display coverage information when visiting. To generate all the files that gcov will check, now you just need to run the program (or the test program generated by googletest). After starting it once (and only once, because if you run it several times, it will add the observable data), you will see the files generated, and now you can call gcov.

It seems that from your (correct) makefile, you simply were not able to complete the β€œrun test program”.

+2
source share

All Articles