转自https://blog.csdn.net/lostaway/article/details/40948841,并针对自己项目做了小幅修改,在此感谢原作者
完整演示项目Github地址:https://github.com/lostaway/EasonCodeShare/tree/master/CMakeGcovSupport
初始项目目录结构:
CMakeGcovSupport
├── CMakeLists.txt
├── bin
├── build
├── include
│ └── name.h
├── libgreeting
│ ├── CMakeLists.txt
│ ├── include
│ │ └── greeting.h
│ └── src
│ ├── CMakeLists.txt
│ ├── greeting.cpp
│ └── yelp.cpp
└── src
├── CMakeGcovSupport.cpp
├── CMakeLists.txt
└── name.cpp
示例程序是个很简单的输出一行问候语的程序。为了演示复杂目录结构下CMake集成Gcov/lcov的方法,故意将输出问候语的函数单独放到了 libgreeting 静态库中。执行如下编译命令:
$ cd CMakeGcovSupport
$ mkdir build
$ cd build
$ cmake -DENABLE_COVERAGE=ON ..
$ gmake all
执行 CMake 外部编译之后,CMake 在 build 目录内为我们生成了 .gcno 文