1. 首先安装lcov,我安装的版本是lcov-1.9

  2. 写一个app.c文件,代码如下:

    #include
    #include
    #include
    void main()
    {
     int a=3;
     int b=4;
     int c=5;

     if(a>b&&a>c)//判断a 是不是最大
     {
      if(b>c)
      {
       printf("a>b>c");
      }else
      {
       printf("a>c>b");
      }
     }
     else if(b>a&&b>c)//判断b 是不是最大
     {
       if(a>c)
      {
       printf("b>a>c");
      }else
      {
       printf("b>c>a");
      }
     }
     else if(c>b&&c>a)//判断 c 是不是最大
     {
       if(a>b)
      {
       printf("c>a>b");
      }else
      {
       printf("c>b>a");
      }
     }
    }

  3. [root@localhost Test]# gcc -fprofile-arcs -ftest-coverage -o myappaa app.c

  4. [root@localhost Test]# ./myapp

  5. [root@localhost Test]# gcov app.c
    File 'app.c'
    Lines executed:58.82% of 17
    app.c:creating 'app.c.gcov'

  6. [root@localhost Test]# ls
    app.c  app.c.gcov  app.gcda  app.gcno  myapp

  7. [root@localhost Test]# lcov -d ./ -o myapp.info -b . -c
    Capturing coverage data from ./
    Found gcov version: 4.4.7
    Scanning ./ for .gcda files ...
    Found 1 data files in ./
    Processing app.gcda
    Finished .info-file creation

  8. [root@localhost Test]# ls
    app.c  app.gcda  app.gcno  myapp  myapp.info

  9. [root@localhost Test]# genhtml -o myapp_restul myapp.info
    Reading data file myapp.info
    Found 1 entries.
    Found common filename prefix "/opt"
    Writing .css and .png files.
    Generating output.
    Processing file zhaogh/Test/app.c
    Writing directory view page.
    Overall coverage rate:
      lines......: 58.8% (10 of 17 lines)
      functions..: 100.0% (1 of 1 function)
      branches...: 33.3% (6 of 18 branches)

  10. sz myapp_restul

  11. 然后打开index就可以查看到代码执行的覆盖率了

  12. lcov for linux 使用_第1张图片