Windows下一般用opencppcoverage计算C++代码覆盖率。
可以直接在Visual Studio上安装
也可以在cmd上运行OpenCppCoverage.exe来实现。
OpenCppCoverage.exe --optimized_build --plugin
--sources D:\TALLivePlatform\TALLivePlatform\
-- D:\TALLivePlatform\TALLivePlatform\build\x64\Release\TALLivePlatform.exe
可以直接生成html,包含数值和饼状图,以及代码覆盖颜色标注。
Linux下可以使用命令
gcov lcov 覆盖c/c++项目入门
cd /data/workspace/C++Coverage/code/ooo;
g++ -c /data/workspace/C++Coverage/code/ooo/oo.cpp -ftest-coverage -fprofile-arcs;
g++ /data/workspace/C++Coverage/code/ooo/oo.o -o /data/workspace/C++Coverage/code/ooo/oo --coverage;
/data/workspace/C++Coverage/code/ooo/./oo;
gcov /data/workspace/C++Coverage/code/ooo/oo.cpp;
lcov -c -o /data/workspace/C++Coverage/code/ooo/oo.info -d .;
genhtml /data/workspace/C++Coverage/code/ooo/oo.info -o /data/workspace/C++Coverage/code/ooo/oo_result"
可以看到语句被覆盖的次数在行头有标注。