Coverage statistics using gcov and lcov

gcov is a tool for code coverage statistics released with gcc. It does not need to be installed separately. It is generally used with its graphical tool lcov.

The following uses ac file compilation and operation to count code coverage to introduce the use of gcov and lcov: (lcov installs itself)

#include <stdio.h>

int add(int a, int b)
{
    int c = a + b;
    return c;
}

int main()
{
    int ret = add(4, 5);
    printf("ret ==> %d\n", ret);
    return 0;
}

编译 :  gcc –o a a.c -fprofile-arcs -ftest-coverage –lgcov 

After compilation, a.gcno file will be generated

Run./a 

    After running, a.gcda file will be generated

Coverage report generation :

lcov -c -d ./ -o coverage. info # -d Specify the compiled directory, scan .gcno and .gcda files, and generate coverage.info file 
genhtml -o report coverage. info # Use coverage.info generated by lcov File forms html visualization coverage report

After the command is executed, the report directory will be generated. Double-click index.html to view the coverage information in the browser.

Related instructions:

  • The .gcno file is generated during the compilation phase. The .gcno file is generated by -ftest-coverage, and it contains information about the line number of the source code to reconstruct the basic block diagram and the corresponding block.
  • The .gcda file will be generated during the run phase. The .gcda file is generated by the compiled file running with the -fprofile-arcs compilation parameter. It contains the number of arc jumps and other summary information, while gcda can only be run in the program. Can only be generated after completion.
  • Since the compilation stage depends on the gcov library, the -lgcov option should be added.
  • If the compilation environment and running environment are the same, the .gcno file and the .gcda file will be generated in the directory where the .c file with the same name is located. For example, ac is located in the / home / workspace / directory, so the generated .gcno and .gcda are also located in the / home / workspace / directory.
  • If the compiling environment and the running environment are different, the same directory structure as the compiling environment will be generated in the running environment to store the .gcda file. For example, in the compilation environment, the directory where .gcno is located is / home / workspace /, then a / home / workspace / directory is also generated in the running environment for storing .gcda files. The .gcda file is copied to the directory where the .gcno file of the same name in the compilation environment is located, so that the gcno and gcda files correspond to each other, and then the coverage report is generated.

Guess you like

Origin www.cnblogs.com/tongyishu/p/12721089.html