初始状态下只有一个源代码文件
[email protected]:testCov> ls test.c [email protected]:testCov>
第一步:编译、链接、执行可执行文件
gcc -o a -coverage test.c -lgcov && ./a
这时会生成所需的记录文件(*.gcda,*.gcno)
[email protected]:testCov> ls a test.c test.gcda test.gcno [email protected]:testCov>
第二步:分析目标文件
[email protected]:testCov> gcov test.c File ‘test.c‘ Lines executed:100.00% of 33 Creating ‘test.c.gcov‘ [email protected]:testCov>
这时会生成*.gcov的文件,该类文件可能会有多个我们只需要打开目标文件对应的gcov文件即可
[email protected]:testCov> vim test.c.gcov
结果如下
-: 0:Source:test.c -: 0:Graph:test.gcno -: 0:Data:test.gcda -: 0:Runs:1 -: 0:Programs:1 -: 1:#include <stdio.h> -: 2: -: 3:char Visit[51]; -: 4:int Ans[51]; -: 5:int N; -: 6:int Cnt[11]; -: 7: #####: 8:void ForEmptyCheck() -: 9:{ #####: 10: puts("Here"); #####: 11:} -: 12: 24: 13:int Sum() -: 14:{ 24: 15: int i, j, Sum = 0; 120: 16: for(i = 0; i < N; ++i) { 96: 17: int min = Ans[i]; 336: 18: for(j = i; j < N; ++j) { 240: 19: if(Ans[j] < min) { 58: 20: min = Ans[j]; -: 21: } -: 22: } 96: 23: Sum += min; -: 24: } 24: 25: ++Cnt[Sum]; 24: 26: return Sum; -: 27:} -: 28: 64: 29:void DFS(int s, int cnt) -: 30:{ -: 31: int i; 64: 32: char over = 1; 64: 33: Visit[s] = 1; 64: 34: Ans[cnt] = s + 1; 320: 35: for(i = 0; i < N; ++i) { 256: 36: if(!Visit[i]) { 60: 37: over = 0; 60: 38: DFS(i, cnt + 1); 60: 39: Visit[i] = 0; -: 40: } -: 41: } 64: 42: if(over) { 120: 43: for(i = 0; i < N; ++i) { 96: 44: printf("%d ", Ans[i]); -: 45: } 24: 46: printf("\t%d\n", Sum()); -: 47: } 64: 48:} -: 49: 1: 50:int main() -: 51:{ -: 52: int i; 1: 53: N = 4; 5: 54: for(i = 0; i < N; ++i) { 4: 55: DFS(i, 0); 4: 56: Visit[i] = 0; -: 57: } 8: 58: for(i=4; i<=10; ++i) -: 59: { 7: 60: printf("%d ", Cnt[i]); -: 61: } 1: 62: puts(""); 1: 63: return 0; -: 64:}
其中ForEmptyCheck()前面有标记字符串“#####”的字样,表示该函数未被调用;
Sum()前面有标记数字“24”,表示该函数被调用了24次,刚好对应着4!=24
时间: 2024-10-30 19:00:15