代码覆盖度测试,gcov

本文介绍如何利用gcov工具结合GCC进行代码覆盖率测试,帮助开发者找出未被测试覆盖的部分,提高代码质量和效率。文章详细展示了gcov的使用过程及如何分析生成的报告。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

copy from gcc manual. 

gcov is a tool you can use in conjunction with GCC to test code coverage in your programs.

gcov is a test coverage program. Use it in concert with GCC to analyze your programs
to help create more efficient, faster running code and to discover untested parts of your
program. You can use gcov as a profiling tool to help discover where your optimization
efforts will best affect your code. You can also use gcov along with the other profiling tool,
gprof, to assess which parts of your code use the greatest amount of computing time.
Profiling tools help you analyze your code’s performance. Using a profiler such as gcov
or gprof, you can find out some basic performance statistics, such as:
 • how often each line of code executes
 • what lines of code are actually executed
 • how much computing time each section of code uses

Running the program will cause profile output to be generated. For each source file
compiled with ‘-fprofile-arcs’, an accompanying ‘.gcda’ file will be placed in the object
file directory.

Running gcov with your program’s source file names as arguments will now produce a
listing of the code along with frequency of execution for each line. For example, if your
program is called ‘tmp.c’, this is what you see when you use the basic gcov facility:

$gcc -fprofile-arcs -ftest-coverage tmp.c (1)
$ ./a.out (2)
$ gcov tmp.c (3)
90.00% of 10 source lines executed in file tmp.c

Creating tmp.c.gcov.

The file ‘tmp.c.gcov’ contains output from gcov. Here is a sample:

-: 0:Source:tmp.c
-: 0:Graph:tmp.gcno
-: 0:Data:tmp.gcda
-: 0:Runs:1
-: 0:Programs:1
-: 1:#include <stdio.h>
-: 2:
-: 3:int main (void)
1: 4:{
1: 5: int i, total;
-: 6:
1: 7: total = 0;
-: 8:
11: 9: for (i = 0; i < 10; i++)
10: 10: total += i;
-: 11:
1: 12: if (total != 45)
#####: 13: printf ("Failure\n");
-: 14: else
1: 15: printf ("Success\n");
1: 16: return 0;
-: 17:}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值