10

I have a CMakeLists.txt file in which I added:

set(CMAKE_CXX_FLAGS "-fprofile-arcs -ftest-coverage -pthread -std=c++11 -O0 ${CMAKE_CXX_FLAGS}")

It is generating the report files in:

project_root/build/CMakeFiles/project.dir/

BUT the files it generates have extentions .cpp.gcno, .cpp.gcda and .cpp.o.

Also, they are not in the same folder as the src files, which are at:

project_root/src/ 

When I move the report files to the src/ folder and execute

$ gcov main.cpp
main.gcno:cannot open notes file

But I get that error message. So I change the .cpp.gcno, .cpp.cdna and cpp.o to .gcno, .gcda and .o and finally I get the following:

gcov main.cpp
Lines executed:86.67% of 15
Creating 'main.cpp.gcov'

I have over 50 files and can't do this manually for each one.

I need to be able to run gcov once for all files and generate report for all files. I don't care where the files are generated.

fedest
  • 1,190
  • 3
  • 15
  • 35

2 Answers2

11

It is generating the report files in: project_root/build/CMakeFiles/project.dir/

This is directory where all additional files are built for 'project' executable.

BUT the files it generates have extentions '.cpp.gcno', '.cpp.gcda' and '.cpp.o'

This is because CMake creates .cpp.o object file from .cpp source (you may see that running make VERBOSE=1. In accordance to -fprofile-arcs option's description, data file has suffix .cpp.gcno.

Also, they are not in the same folder as the src files

Data files are created in the same directory with object file.


Actually, created files are still work, if you call

gcov main.cpp.gcno

from the directory with .gcno files.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Well, you are right, it does work. I must have tested it somewhere else then. Do you know of a way to geta general report of the files not having to run it file by file? AND, how to avoid the report of stl files, like "stl_map.h" and from the header files of my project? – fedest Jun 22 '16 at 23:22
  • `Do you know of a way to get a general report of the files ...` - probably, you want *lcov* utility. Also, you may call `gcov *.gcno`, so it will collect all data files in directory at once. `how to avoid the report of stl files, like "stl_map.h" and from the header files of my project?` - I don't know easy way for doing this. (Complex way is to parse intermediate format of *gcov* output and drop everything related to headers). You may ask another question about this on Stack Overflow. – Tsyvarev Jun 23 '16 at 07:46
  • Thank, I ended up using lcov for the whole generation, though it still gets the coverage of .h files, I'll make another post asking that soon. – fedest Jun 24 '16 at 00:53
  • "This is because CMake creates .cpp.o object file from .cpp source" It'd be nice to have an explanation of how to disable that. – Nic Dec 20 '18 at 00:46
  • I know why they remains source extension for the object files (for differentiate object files created from sources on different languages), but I don't know how to disable this. You may create new question on Stack Overflow about that. – Tsyvarev Dec 20 '18 at 07:23
5

Apparently the standard CMake behavior to add an extension to give .cpp.o can be changed to replace an extension to give .o by using:

set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE ON)
Mathijs
  • 51
  • 1
  • 1