我需要在我正在处理的共享库上使用 gcov
执行一些测试覆盖。
问题是 libtool
将目标文件从 my-name.c
重命名为 libmylib_la-my-name.lo
和 gcov
无法处理该转换。每次运行它时,都会生成错误 cannot open notes file
。
如果我在构建 gcov
后手动将 my-name.c
重命名为 libmylib_la-my-name.c
工作正常,那么除了文件名修改之外没有其他问题。
附录
尝试提供一个最小的工作示例,我发现只有在设置了 lib..._la_CFLAGS
时才会发生文件名重整(以及设置为空值时) ).
cat <<EOT > configure.ac
AC_INIT(sample,0.0.1)
AC_CONFIG_SRCDIR(configure.ac)
AM_INIT_AUTOMAKE(foreign)
LT_INIT
AC_PROG_CC
AC_CONFIG_FILES(Makefile)
AC_OUTPUT
EOT
cat <<EOT > Makefile.am
lib_LTLIBRARIES=libsample.la
libsample_la_SOURCES=sample.c
# The following line triggers the filename mangling (libsample_la-sample.lo instead of sample.lo)
libsample_la_CFLAGS=
EOT
touch sample.c && autoreconf -if && ./configure && make
有没有办法避免 libtool 操作的文件名重整或让 gcov
理解文件名重整方案?
最佳答案
Gcov gcda
和gcno
文件以目标文件命名。您可以直接在目标文件上从源目录运行 gcov
或者您可以使用 gcov
的 -o
选项指定目标文件和相应的 gcov
文件。
例如,我有一个构建共享库的小项目。我将 gcov
标志传递给 make 命令:
make CFLAGS="-O0 --coverage" LDFLAGS=--coverage
目标文件和相应的gcno
文件在src/.libs
文件夹中创建:
$ ls -la src/.libs
libtest_la-test.o
libtest_la-test.gcno
源文件在src
文件夹中
$ ls src/
test.c
接下来我运行我的测试套件并创建 gcda
文件:
$ ls -la src/.libs
libtest_la-test.o
libtest_la-test.gcno
libtest_la-test.gcda
现在我可以进入src
目录并运行gcov
,指定目标文件名:
$ gcov -o .libs/libtest_la-test.o test.c
File ‘test.c’
Lines executed:27.08% of 96
Creating ‘test.c.gcov'
也可以只在目标文件上运行 gcov
:
$ gcov .libs/libtest_la-test.o
File ’test.c’
Lines executed:27.08% of 96
Creating ’test.c.gcov'
或者甚至只是指定目标文件和 gcov
文件的基本名称:
$ gcov .libs/libtest_la-test
File ’test.c’
Lines executed:27.08% of 96
Creating ’test.c.gcov'
但我会建议另一种对我来说非常有效的自动化方法,使用 lcov .我从指定源文件和目标文件路径的顶级目录调用它:
$ lcov --base-directory src --directory src/.libs/ --capture --output-file gcov.info
Capturing coverage data from src/.libs/
Found gcov version: 4.8.2
Scanning src/.libs/ for .gcda files ...
Found 10 data files in src/.libs/
Processing .libs/test_la-test.gcda
[…]
Finished .info-file creation
$ genhtml -o html/coverage gcov.info
Reading data file gcov.info
Found 10 entries.
Found common filename prefix "/usr/src/libtest”
Writing .css and .png files.
Generating output.
Processing file src/test.c
[…]
Writing directory view page.
Overall coverage rate:
lines......: 56.1% (2098 of 3737 lines)
functions..: 68.8% (139 of 202 functions)
现在 html/coverage
目录包含可以在网络浏览器中轻松分析的 html 文件。
关于c - Libtool 为对象添加前缀,但 gcov 要求它们没有前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28004184/