使用lcov生成代码覆盖率报告时出现错误

4

我正在尝试在我的项目上运行覆盖率测试,但在升级到Ubuntu 16.04之后,出现了以下错误:


Deleted 665 files
Writing data to coverage.info.cleaned
lcov: ERROR: cannot write to coverage.info.cleaned!
CMakeFiles/coverage.dir/build.make:57: recipe for target 'CMakeFiles/coverage' failed
make[3]: *** [CMakeFiles/coverage] Error 13
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/coverage.dir/all' failed
make[2]: *** [CMakeFiles/coverage.dir/all] Error 2
CMakeFiles/Makefile2:74: recipe for target 'CMakeFiles/coverage.dir/rule' failed
make[1]: *** [CMakeFiles/coverage.dir/rule] Error 2
Makefile:129: recipe for target 'coverage' failed
make: *** [coverage] Error 2
enter code here

更新之前,我运行覆盖率时没有任何问题。

你尝试过执行 make clean 命令吗? - Gluttton
当然,我甚至重新制作了覆盖率目录。 - Ortal
2
找到了问题,我的情况是在最后删除了所有不必要的输出,并将它们重新写入到另一个文件中。更新到16.04后,写入新数据的新位置位于根目录。通过设置新目标来解决了这个问题。 - Ortal
谢谢!将其作为已接受的答案。那帮助我解决了我的问题。顺便说一下,那肯定是个错误... - Petter
1个回答

5

当向 lcov 传递文件时,使用绝对路径而不是相对路径是否有帮助?

我遇到了类似的问题,lcov也无法写入文件。不确定这是否是 lcov 的一个 bug,但问题在于它混淆了相对路径:

lcov -a test_fast_cxxtest_gcov__base.info -a test_fast_cxxtest_gcov__test.info \
     -o test_fast_cxxtest_gcov__total.info
Combining tracefiles.
Reading tracefile test_fast_cxxtest_gcov__base.info
Reading tracefile test_fast_cxxtest_gcov__test.info
lcov: WARNING: function data mismatch at /home/phil/ghost/constants.h:1862
Writing data to test_fast_cxxtest_gcov__total.info
lcov: ERROR: cannot write to test_fast_cxxtest_gcov__total.info!

使用strace查看运行情况,我们发现它在多个位置上执行了chdir("/")命令,将工作目录更改为/。这就解释了为什么它无法写入文件。

一个解决方法是使用绝对路径。例如,如果你正在使用GNU make,则可以使用abspath命令:

lcov -a $(abspath test_fast_cxxtest_gcov__base.info) \
     -a $(abspath test_fast_cxxtest_gcov__test.info) \
     -o $(abspath test_fast_cxxtest_gcov__total.info)

在更改之后,终于可以写入该文件了。

尝试使用--base-directory或者--directory选项设置目录并未产生效果(就我所见,版本为1.12)。

这个问题不仅存在于Ubuntu中,我在Arch Linux上也遇到了同样的问题。这可能是1.12中引入的退化,因此我进行了报告(参见issue #77630)。

更新:Lcov不是GCC的一部分,所以原始的bug报告被关闭了,但我从Lcov邮件列表中得到了答复。问题已经在提交632c25中解决。基于Arch Linux的发行版用户可以尝试使用aur/lcov-git获取最新的快照。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接