Gcov in large projects (static libraries, ...)

I am working on a larger project that has the following catalog layout:

Source MyA aa.cpp ab.cpp ac.cpp MyB ba.cpp bb.cpp bc.cpp MyTest testaa.cpp testab.cpp testac.cpp testba.cpp testbb.cpp testbc.cpp main.cpp Build MyA aa.o ab.o ac.o libMyA.a (static library) MyB ba.o bb.o bc.o libMyB.a (static library) MyTest testaa.o testab.o testac.o testba.o testbb.o testbc.o MyTest (executable) 

After compiling with -fprofile-arcs -ftest-coverage I run the MyTest application inside the Build / MyTest directory. As expected, there are * .gcno and * .gcda files in the Build directory. After running gcov, different * .gcov files are created inside the MyTest directory, but, unfortunately, not for everyone inside MyA and MyB, although each function is called inside these two libraries. I tried different options, but somehow I can’t create useful (correct) * .gcov files with this layout.

If I copy each cpp inside the same directory and repeat the steps, everything works as expected, and coverage analysis is perfect.

+4
source share
3 answers
  • You must specify the source files as absolute paths to g ++ / gcc. Do not use relative paths with ".." or "foo / bar.cpp", otherwise you will get errors such as "geninfo: WARNING: no data found for XXXX".

  • Do not include header files on the command line in g ++ / gcc. In addition, you will receive errors "marker mismatch with the graphic file."

So, the following should work if there are multiple directories:

 g++ --coverage -DDEBUG -g3 heyo.cpp /app/helper/blah.cpp /app/libfoo/foo.cpp -o program ./program lcov --directory . --capture --output-file app.info genhtml --output-directory cov_htmp app.info 

Or, if you are in a Makefile that already uses relative paths, it’s convenient to use:

 g++ --coverage -DDEBUG -g3 $(abspath heyo.cpp helper/blah.cpp ../foo/bar/baz.cpp) -o program 
+6
source

To preserve the directory structure, you need to run gcov once in each folder of the source file, but use the -o option to tell gcov where the data files are.

I think it should be like this:

 gcov -o ../../Build/MyA *.cpp 

I have a project with a similar source file structure, but I allow compiler dump file files, etc. to source folders. Then I run gcov several times from the root folder, once for each source file, but I specify the relative path of the source file and use the -o option to specify the relative folder as follows:

 gcov -o Source/MyA Source/MyA/aa.cpp 
+4
source

If you have conducted a thorough and routine testing of your product or application and spent a lot of effort on it. If your goal is to get a code coverage report using lcov and gcov, but gcno files were deleted by mistake. You can regenerate gcno files by recompiling the code, but it will be generated with a new timestamp and gcov will report an error indicating “marker mismatch with the graphic file” and no code coverage report will be generated. This will result in all of your testing efforts being wasted.

There is a shortcut to generate a code coverage report. This is just a workaround, and you cannot rely on it all the time. It is recommended that you save * .gcno files until testing is complete.

Write down your version of gcc (gcc -v) and download its source code from one of the mirror sites. For example, ftp://gd.tuwien.ac.at/gnu/sourceware/gcc/releases/gcc-4.4.6/gcc-4.4 .6.tar.bz2

After extracting the downloaded gcc file, the folder structure will be as follows: GCC-4.4.6 GCC-4.4.6 / GCC

If you go straight into gcc-4.4.6 / gcc and try to do it. / configure and compile (make), then you will encounter a problem below build / genmodes -h> tmp-modes.h / bin / sh: build / genmodes: There is no such file or directory

The decision is made. / configure and make from gcc-4.4.6, and no errors will be shown related to genmodes. This will compile all modules, including gcc. You may need to install the mpfr and gmp modules that gcc needs if there is any error shown. / configure

goto gcc-4.4.6 / gcc / gcov.c and the comment below the lines, then recompile with the command

 /* if (tag != bbg_stamp) { fnotice (stderr, "%s:stamp mismatch with graph file\n", da_file_name); goto cleanup; }*/ 

An example of the path of the new gcov binary after compilation is gcc-4.4.6 / host-x86_64-unknown-linux-gnu / gcc / gcov

Put this binary in / usr / bin and update the code coverage report with the command as shown below. lcov --capture --directory./--output-file coverage.info; genhtml coverage.info --output-directory / var / www / html / coverage

Now you should not get the error "marker mismatch with the graphic file", and you will get a report on code coverage

-1
source

Source: https://habr.com/ru/post/1347151/


All Articles