gcov 与 CMake 使用单独的构建目录

发布于 2024-11-08 15:46:26 字数 315 浏览 3 评论 0原文

我正在努力获取 gcov 的覆盖范围信息。编译和链接期间没有错误,但是当我运行可执行文件时,没有生成覆盖率数据。

我使用带有单独构建目录的 CMake,以这种方式将标志传递给编译器和链接器:

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

可执行文件是否期望源代码位于特定位置? 我必须在 CMakeLists.txt 中添加什么才能让事情顺利进行?

亲切的问候, 比约恩

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聽兲甴掵 2024-11-15 15:46:26

CMake 似乎将代码覆盖率(*.gcda、*.gcdo)文件与项目的目标文件放在一起。如果您的可执行文件被命名为“tester”,那么它们将出现在以下路径中,

${CMAKE_BINARY_DIR}/CMakeFiles/tester.dir/

但 CMake 似乎以与 gcov 不太兼容的方式命名源文件。例如,如果我有一个名为“mytestprog.cpp”的源文件,它将

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

在 gcov 似乎期望的地方

mytestprog.gcda
mytestprog.gcdno

构建,我不太确定如何修复它。我尝试过使用 LCov 来代替,并且“似乎”有效,但我不确定它是否有效。

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.

独守阴晴ぅ圆缺 2024-11-15 15:46:26

德尔塞弗指出了问题所在。

解决方案 1:您可以要求 cmake 将目标文件命名为 main.o 而不是 main.cpp.o等使用未记录的CMAKE_CXX_OUTPUT_EXTENSION_REPLACE开关:

cmake -DCMAKE_CXX_OUTPUT_EXTENSION_REPLACE=ON ...

解决方案2:如果您不需要.gcov文件,您可以调用lcov 来自 build 目录:

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

您将在 out 目录中找到 html 形式的覆盖率信息。

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.

悲凉≈ 2024-11-15 15:46:26

不确定您从哪里获得 --coverage ,但这些是我在 Linux 上使用 gcc 和 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")

以下是 gcc --help --verbose 不得不说一下这些选项:

-ftest-覆盖率创建
“gcov”所需的数据文件

-fprofile-弧插入
基于弧的程序分析代码

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

好久不见√ 2024-11-15 15:46:26

您不需要将 --coverage 传递给链接器。 --coverage 将 -fprofile-arcs -ftest-coverage 传递给编译器,将 -lgcov 传递给链接器。

您确定它没有创建任何 gcdo 或 gcda 文件吗?您在哪里寻找这些文件?它应该将每个目标文件的 gcov 文件放入与目标文件相同的目录中。在构建目录的顶部查找 .gcda 文件。如果没有显示任何内容,则 gcov 可能未链接到。运行以下命令以查看是否已链接:

nm name_of_binary | grep "gcov"

如果已链接到,则 gcov 可能无权将文件写入运行可执行文件的位置。如果获得许可,那我就难住了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文