问题背景介绍:
在Linux系统中,利用文本编辑器创立一个文件cov.c,文件内容如下:
#include<stdio.h>
int main (void)
{
int i;
for( i=1;i<10;i++)
{
if(i%3==0)
printf("%d is divisible by 3\n",i);
if(i%11==0)
printf("%d is divisible by 11\n",i);
}
return 0;
}
在该文件所处目录中使用“在终端打开”;
在命令行输入:gcc -Wall -fprofile-arcs -ftest-coverage cov.c;
在命令行输入:./a.out ;
在命令行输入:gcov cov.c
得到:
cov.gcno:cannot open notes file
cov.gcda:cannot open data file, assuming not executed
No executable lines
解决方法:
(可以先将之前目录中除cov.c以外其它文件都删除)
在命令行输入:gcc -Wall -fprofile-arcs -ftest-coverage cov.c -o cov;
在命令行输入:./cov;
在命令行输入:gcov cov.c
得到:
File 'cov.c'
Lines executed:85.71% of 7
Creating 'cov.c.gcov'
Lines executed:85.71% of 7
原目录中多出一文件——cov.c.gcov,内容为:
-: 0:Source:cov.c
-: 0:Graph:cov.gcno
-: 0:Data:cov.gcda
-: 0:Runs:1
-: 1:#include<stdio.h>
-: 2:
1: 3:int main (void)
-: 4:{
-: 5: int i;
-: 6:
10: 7: for( i=1;i<10;i++)
-: 8: {
9: 9: if(i%3==0)
3: 10: printf("%d is divisible by 3\n",i);
9: 11: if(i%11==0)
#####: 12: printf("%d is divisible by 11\n",i);
-: 13: }
-: 14:
1: 15: return 0;
-: 16:
-: 17: }