gcov 与 CMake 使用单独的构建目录
我正在努力获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CMake 似乎将代码覆盖率(*.gcda、*.gcdo)文件与项目的目标文件放在一起。如果您的可执行文件被命名为“tester”,那么它们将出现在以下路径中,
但 CMake 似乎以与 gcov 不太兼容的方式命名源文件。例如,如果我有一个名为“mytestprog.cpp”的源文件,它将
在 gcov 似乎期望的地方
构建,我不太确定如何修复它。我尝试过使用 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 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
where as gcov seems to expect
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.
德尔塞弗指出了问题所在。
解决方案 1:您可以要求
cmake
将目标文件命名为main.o
而不是main.cpp.o
等使用未记录的CMAKE_CXX_OUTPUT_EXTENSION_REPLACE
开关:解决方案2:如果您不需要
.gcov
文件,您可以调用lcov 来自 build 目录:
您将在
out
目录中找到 html 形式的覆盖率信息。Delcypher pointed out the problem.
Solution 1: you can ask
cmake
to name object files asmain.o
instead ofmain.cpp.o
etc. using the undocumentedCMAKE_CXX_OUTPUT_EXTENSION_REPLACE
switch:Solution 2: if you do not need the
.gcov
files you can calllcov
from the build directory:You will find the coverage information in the
out
directory in html form.不确定您从哪里获得
--coverage
,但这些是我在 Linux 上使用 gcc 和 gcov 获取覆盖率信息的参数:以下是
gcc --help --verbose
不得不说一下这些选项:Not sure where you got
--coverage
from, but these are the arguments I use on Linux to get coverage information using gcc and gcov:Here's what
gcc --help --verbose
has to say about those options:您不需要将 --coverage 传递给链接器。 --coverage 将 -fprofile-arcs -ftest-coverage 传递给编译器,将 -lgcov 传递给链接器。
您确定它没有创建任何 gcdo 或 gcda 文件吗?您在哪里寻找这些文件?它应该将每个目标文件的 gcov 文件放入与目标文件相同的目录中。在构建目录的顶部查找 .gcda 文件。如果没有显示任何内容,则 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:
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.