Gcov is a code coverage tool that can be used in C/s + +, and is a built-in tool for GCC. Here's how to use Gcov to collect code coverage information.
To collect code overwrite information with Gcov, you need to add these 2 options "-fprofile-arcs-ftest-coverage" when GCC compiles the code, and compile this simple program
Gcc-fprofile-arcs-ftest-coverage Hello.c-o Hello
After compiling, you get an executable hello and hello.gcno file, and when you compile the file with GCC, the. gcno file is generated with the "-ftest-coverage" parameter, which contains information such as the program block and line number.
Next, you can run this Hello program
./hello 5
./hello 12
A HELLO.GCDA file is generated after the run finishes and is generated if an executable file is compiled with the "-fprofile-arcs" parameter and runs at least once. This file contains information about the basic block jumps of the program. Next you can generate code coverage information with Gcov:
Gcov hello.c
2 files Hello.c.gcov and Myfunc.c.gcov are generated after the run finishes. Open the message to look inside:
-: 0:SOURCE:MYFUNC.C
-: 0:graph:hello.gcno
-: 0:DATA:HELLO.GCDA
-: 0:runs:1
-: 0:programs:1
-: 1: #include
-: 2:
-: 3:void Test (int count)
1:4:{
-: 5:int i;
10:6: for (i = 1; i < count; i++)
-: 7: {
9:8: if (i% 3 = = 0)
3:9: printf ("%d is divisible by 3 \ n", i);
9:10:if (i% 11 = = 0)
#####: 11:printf ("%d is divisible by one \ n", i);
9:12:if (i% 13 = = 0)
#####: 13:printf ("%d is divisible by", I);
-: 14:}
1:15:}
is marked as # # # #的代码行就是没有被执行过的, the code covers the correct information, but it is really a cup of tea to read these words. Don't worry, there is another tool called Lcov, you can use the program to parse these obscure characters, the final output to the HTML format of the report, very good!
Lcov-d. -T ' Hello Test '-o ' hello_test.info '-B. -C
Specify Lcov in the current directory "." To find the code to cover the information, output as ' hello_test.info ', this hello_test.info is an intermediate result, need to use it genhtml to deal with, genhtml is a tool inside the Lcov.
Genhtml-o result Hello_test.info
Specifies that the output directory is result. A full HTML report is generated, a connection is made, and the directory is attached to a directory of any Web server, so you can see the report.
My project is using Lcov, and using Makefile, the main usage is as follows:
To define a macro file in makefile:
Profile
- Ifeq ($ (profile), 1)
- CFLAGS + =-fprofile-arcs
- CFLAGS + =-ftest-coverage
- Cxxflags + =-fprofile-arcs
- Cxxflags + =-ftest-coverage
- Ldflags + =-fprofile-arcs
- Ldflags + =-ftest-coverage
- Ldflags + =-lgcov
- Libldflags + =-fprofile-arcs
- Libldflags + =-ftest-coverage
- Libldflags + =-lgcov
- endif
Open this macro at compile time: profile=1 make RTM debug=0
Then the *.GCDA file is generated after running.
Use the following command to generate the report:
- Lcov-d. -B. -c-o $rtmcovfile >/dev/null
- #sed-i-e ' s#/home/mac_ci/hudson/home/jobs/trunk_rtm/workspace/trunk#\. #g ' $rtmcovfile
- #-Keep Sstddps folders in the
- Lcov-r $rtmcovfile "*/ss_macdata/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "*/ss_macpswmp/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "*/sscommon/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "*/ssdata/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "*/ssdcmps/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "*/sspscommon/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "*/sstestmodel/*"-O $rtmcovfile
- #-Remove unneeded paths from coverage
- Lcov-r $rtmcovfile "/build/ltesdkroot/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "*/sc_dsp_common/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "*/c_test/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "*/t_tools/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "*/env/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "*/i_interface/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "/usr/*"-O $rtmcovfile
- Lcov-r $rtmcovfile "/opt/*"-O $rtmcovfile
- MV $rtmcovfile $PROJECT _root/c_test/sc_mac/maclinuxrtm/logs
- CD $PROJECT _root/c_test/sc_mac/maclinuxrtm/logs
- Genhtml-o tdd_ut_rtm_tests_coverage $rtmcovfile >/dev/null
Test Results Overview
Coverage for a specific file
C + + code coverage tool Gcov, Lcov