gcov with CMake using a separate build directory

☆樱花仙子☆ 提交于 2019-12-30 02:08:20

问题


I'm struggling to get coverage information for gcov. No errors during compilation and linking, but when I run the executable, no coverage data is produced.

I'm using CMake with a separate build directory, passing flags to the compiler and linker in this way:

add_definitions(--coverage)
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} " --coverage")

Does the executable expect the source code to be in a specific location? What do I have to add to my CMakeLists.txt to get things going?

Kind regards, Bjoern


回答1:


CMake seems to put the code coverage (*.gcda, *.gcdo) files with the object files of your project. If your executable was named "tester" then they would appear in the following path

${CMAKE_BINARY_DIR}/CMakeFiles/tester.dir/

CMake seems to name the source files in a way that isn't very compatible with gcov though. For example if I had a source file called "mytestprog.cpp" it would be build

mytestprog.cpp.o
mytestprog.cpp.gcda
mytestprog.cpp.gcdno

where as gcov seems to expect

mytestprog.gcda
mytestprog.gcdno

I'm not really sure how to fix it. I've tried using LCov instead and that "appeared" to work but I'm not really sure if it did.




回答2:


Delcypher pointed out the problem.

Solution 1: you can ask cmake to name object files as main.o instead of main.cpp.o etc. using the undocumented CMAKE_CXX_OUTPUT_EXTENSION_REPLACE switch:

cmake -DCMAKE_CXX_OUTPUT_EXTENSION_REPLACE=ON ...

Solution 2: if you do not need the .gcov files you can call lcov from the build directory:

lcov --capture --directory . --output-file coverage.info
genhtml coverage.info --output-directory out

You will find the coverage information in the out directory in html form.




回答3:


Not sure where you got --coverage from, but these are the arguments I use on Linux to get coverage information using gcc and gcov:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS
    "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")

Here's what gcc --help --verbose has to say about those options:

-ftest-coverage Create data files needed by "gcov"

-fprofile-arcs Insert arc-based program profiling code




回答4:


You don't need to pass --coverage to the linker. --coverage will pass -fprofile-arcs -ftest-coverage to the compiler and -lgcov to the linker.

Are you sure that it isn't creating any gcdo or gcda files? Where are you looking for these files? It should put the gcov file for each object file into the same directory as the object file. Do a find for .gcda files at the top of your build directory. If nothing shows up, gcov might not be getting linked in. Run the following command to see if it is:

nm name_of_binary | grep "gcov"

If it is getting linked in, then gcov might not have permission to write files to where you are running the executable. If it has permission, then I am stumped.



来源:https://stackoverflow.com/questions/6089140/gcov-with-cmake-using-a-separate-build-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!