GCOV和GCOVR:编译后生成否.GCNO文件

发布于 2025-02-05 18:48:26 字数 1461 浏览 2 评论 0 原文

我正在尝试在Windows系统中的单元测试项目中获得代码覆盖率。

描述

使用 -fprofile-arcs -ftest-Coverage 编译后,我发现执行文件已生成并正常工作。但是,文件夹中没有任何.gcno文件。因此,我无法通过GCOVR正确输出覆盖范围报告。

软件版本

GCC 8.1.0/gcov 8.1.0/gcovr 5.1/python 3.10.2

步骤

这是我在整个过程中所做的。如果有问题,请帮助我。

  1. 一个文件夹中只有.c和.h文件

  2. 使用GCC

    编译我的项目

GCC -WALL -WNO -INKNOWN -PRAGMAS -FCOMPARE -DEBUG -SECOND -FPROFILE -ARCS -FTEST -COVERAGE -DUTEST.C CUTEST.C bzr2.c bzr2.c bzr2.c bzr2_test.c -o beta.c -o beta.exe.c -o beta.exe

  1. 然后我在文件夹中得到了beta.exe。

  2. 运行beta.exe后,我的测试结果(所有测试均已通过。)在命令行窗口中显示。此外,还有.gcda文件,带有与我的.c文件相同的文件名。

  3. 然后我运行 gcovr -r。,结果如下所示。我认为为什么GCOVR无法显示覆盖范围信息的环境是编译我的项目后没有生成的.gcno文件。但是我不明白为什么以及如何解决这个问题。

------------------------------------------------------------------------------
                           GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File                                       Lines    Exec  Cover   Missing
------------------------------------------------------------------------------
------------------------------------------------------------------------------
TOTAL                                          0       0    --%
------------------------------------------------------------------------------

感谢您的时间!

I am trying to get code coverage in my unit test project in windows system.

Description

After compiling with -fprofile-arcs -ftest-coverage, I found out the execution file is generated and works fine. However there's no any .gcno files in the folder. So I cannot output the coverage report properly by gcovr.

Software version

gcc 8.1.0/gcov 8.1.0/gcovr 5.1/python 3.10.2

Steps

Here's what I've done during the whole process. Please help me if there's something wrong.

  1. There are only .c and .h files in one folder

  2. Compile my project using gcc

gcc -Wall -Wno-unknown-pragmas -fcompare-debug-second -fprofile-arcs -ftest-coverage -DUTEST AllTests.c CuTest.c BZR2.c BZR2_test.c -o beta.exe

  1. Then I got beta.exe in the folder.

  2. After runing beta.exe, there's my test result(All tests are passed.) showing in the command line window. Besides there're .gcda files with the same filename as my .c files.

  3. Then I run gcovr -r ., the result is showing below. I think the reson why gcovr can't show the coverage information is there's no any .gcno files generated after compiling my project. But I don't understand why and how to solve this.

------------------------------------------------------------------------------
                           GCC Code Coverage Report
Directory: .
------------------------------------------------------------------------------
File                                       Lines    Exec  Cover   Missing
------------------------------------------------------------------------------
------------------------------------------------------------------------------
TOTAL                                          0       0    --%
------------------------------------------------------------------------------

Thanks for your time!

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

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

发布评论

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

评论(1

归途 2025-02-12 18:48:26

删除 -fcompare-debug-second 选项。它用于调试编译器本身,并导致编译器

沉默警告,并省略其他将导致编译器产生输出或标准输出作为副作用的选项。

(请参阅:

创建GCNO文件是如此副作用。


一般提示:

代替 -fprofile-arcs -test-coverage 您可以简单地使用 - coverage 选项。

当您一口气编译多个源文件时,GCC试图找出中间文件的文件名,并自动为诸如GCNO文件(例如GCNO文件)派生一些名称。至少在

为了单独编译所有文件,我们将使用结构:

OPTIONS="-Wall -Wno-unknown-pragmas --coverage -DUTEST"

# compile the individual compilation units
gcc -c $OPTIONS AllTests.c -o AllTests.o
gcc -c $OPTIONS BZR2.c -o BZR2.o
gcc -c $OPTIONS BZR2_test.c -o BZR2_test.o

# we should now have three gcno files
ls *.gcno

# link the final executable
gcc $OPTIONS CuTest.o BZR2.o BZR2_test.o -o beta.exe

在这一点上,使用构建系统通常是合适的,例如,通过编写makefile:

CFLAGS += -Wall -Wno-unknown-pragmas --coverage -DUTEST

SOURCES = AllTests.c BZR2.c BZR2_tests.c
OBJECTS = $(SOURCES:.c=.o)

beta.exe: $(OBJECTS)
    $(CC) $(CFLAGS) $^ -o $@

Remove the -fcompare-debug-second option. It is used for debugging the compiler itself, and causes the compiler

to silence warnings, and omitting other options that would cause the compiler to produce output to files or to standard output as a side effect.

(see: https://gcc.gnu.org/onlinedocs/gcc-8.5.0/gcc/Developer-Options.html)

Creation of gcno files is such a side effect.


General tips:

Instead of -fprofile-arcs -test-coverage you can simply use the --coverage option.

When you compile multiple source files in one go, then GCC tries to figure out file names for intermediate files, and also automatically derives some name for secondary outputs like gcno files. This used to be somewhat unintuitive, at least until reasonable behaviour was implemented in GCC 11.

To compile all of the files individually, we would use the structure:

OPTIONS="-Wall -Wno-unknown-pragmas --coverage -DUTEST"

# compile the individual compilation units
gcc -c $OPTIONS AllTests.c -o AllTests.o
gcc -c $OPTIONS BZR2.c -o BZR2.o
gcc -c $OPTIONS BZR2_test.c -o BZR2_test.o

# we should now have three gcno files
ls *.gcno

# link the final executable
gcc $OPTIONS CuTest.o BZR2.o BZR2_test.o -o beta.exe

At this point, it's typically appropriate to use a build system, for example by writing a Makefile:

CFLAGS += -Wall -Wno-unknown-pragmas --coverage -DUTEST

SOURCES = AllTests.c BZR2.c BZR2_tests.c
OBJECTS = $(SOURCES:.c=.o)

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