The GCC/g ++ comes with the gcov tool to automatically generate an executionCodeGcda. You only need to compile the macro-fprofile-arcs-ftest-coverage to generate the gcno file.
Specific information is visible
Http://gcc.gnu.org/onlinedocs/gcc-3.3.6/gcc/Gcov-Intro.html#Gcov-Intro
This section describes how to use the file generated by gcov to generate a more intuitive web page tool lcov.Http://ltp.sourceforge.net/coverage/lcov.phpDownload this tool. Let's take a simple example to see how to use this tool.
1. Install lcov
# Tar-zxvf lcov-1.10.tar.gz
# Make install
In this way, we can have two tools, lcov genhtml, which can be used to directly run the command to see the help information-help you understand ..
2. Test the CPP File
# Include "stdio. H "# include" string. H "int test (int I) {if (I> 0) {printf (" IU> 0 \ n ");} else {printf ("I <0 \ n") ;}return 0 ;}int main () {test (1); Return 0 ;}
A file named test. gcno is generated.
3. RunProgram
./OK
Generate a gcda File
4. Execute lcov to generate a file
Lcov-D.-t OK-O coverage.info-B.-C
-D directory with execution Program (OK) after-O indicates file generation
Since then, the conditions for generating web pages have been fully met.
5. Generate a webpage
Genhtml-o Result coverage.info
-O path + conversion information
The page information is as follows: You can clearly see which branches of the current Code have not been reached.
Summary:
Code coverage helps us perform unit tests to a certain extent. We can perform the above tests on some test cases to see if the test cases can well cover all the branches of the Code, further improve code Stability.