- 首先安装lcov,我安装的版本是lcov-1.9
- 写一个app.c文件,代码如下:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main()
{
int a=3;
int b=4;
int c=5;if(a>b&&a>c)//判断a 是不是最大
{
if(b>c)
{
printf("a>b>c");
}else
{
printf("a>c>b");
}
}
else if(b>a&&b>c)//判断b 是不是最大
{
if(a>c)
{
printf("b>a>c");
}else
{
printf("b>c>a");
}
}
else if(c>b&&c>a)//判断 c 是不是最大
{
if(a>b)
{
printf("c>a>b");
}else
{
printf("c>b>a");
}
}
} - [[email protected] Test]# gcc -fprofile-arcs -ftest-coverage -o myappaa app.c
- [[email protected] Test]# ./myapp
- [[email protected] Test]# gcov app.c
File ‘app.c‘
Lines executed:58.82% of 17
app.c:creating ‘app.c.gcov‘ - [[email protected] Test]# ls
app.c app.c.gcov app.gcda app.gcno myapp - [[email protected] Test]# lcov -d ./ -o myapp.info -b . -c
Capturing coverage data from ./
Found gcov version: 4.4.7
Scanning ./ for .gcda files ...
Found 1 data files in ./
Processing app.gcda
Finished .info-file creation - [[email protected] Test]# ls
app.c app.gcda app.gcno myapp myapp.info - [[email protected] Test]# genhtml -o myapp_restul myapp.info
Reading data file myapp.info
Found 1 entries.
Found common filename prefix "/opt"
Writing .css and .png files.
Generating output.
Processing file zhaogh/Test/app.c
Writing directory view page.
Overall coverage rate:
lines......: 58.8% (10 of 17 lines)
functions..: 100.0% (1 of 1 function)
branches...: 33.3% (6 of 18 branches) - sz myapp_restul
- 然后打开index就可以查看到代码执行的覆盖率了
时间: 2025-01-08 20:27:56