您的位置:首页 > 运维架构 > Linux

gcc 中参数-ftest-coverage -fprofile-arcs 使用

2014-08-26 15:00 204 查看
利用gcov检测覆盖率

例子:

#include <stdio.h>

int _abs(int a)

{
if (a < 0) {
a = 0-a;

}
return a;

}

int _max(int a, int b)

{
int max = a;
if (a < b) {
max = b;
}
return max;

}

void sort( int list[], int size)

{
int i, j, temp, swap = 1;
while (swap) {
swap = 0;
for ( i = (size-1) ; i >= 0 ; i-- ) {
for ( j = 1 ; j <= i ; j++ ) {
if ( list[j-1] > list[j] ) {
temp = list[j-1];
list[j-1] = list[j];
list[j] = temp;
swap = 1;
}
}
}
}

}

int main() 

{
int x = 4;
int a = 15,b = 20;

int theList[10]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int i;
sort( theList, 10 );

for (i = 0 ; i < 10 ; i++) { 
printf("%d\n", theList[i]);

}

printf("Hello Ubuntu!!\n");

printf("abs(%d) is %d\n",x,_abs(x));
printf("max(%d,%d) is %d\n",a,b,_max(a,b));

return 0;

}

编译

gcc -fprofile-arcs -ftest-coverage Hello.c

运行:

./a

1

2

3

4

5

6

7

8

9

10

Hello Ubuntu!!

abs(4) is 4

max(15,20) is 20

$ gcov -b Hello.c

File 'Hello.c'

Lines executed:96.88% of 32

Branches executed:100.00% of 14

Taken at least once:85.71% of 14

Calls executed:100.00% of 7

Creating 'Hello.c.gcov'

$ gcov  Hello.c

File 'Hello.c'

Lines executed:96.88% of 32

Creating 'Hello.c.gcov'

以上是执行的效果,自己作为以后查看备份在这里。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux gcc