linuxC++写一个可以用于测试lcov和gcov的源码,以及G++命令
时间: 2024-02-28 18:55:29 浏览: 31
以下是一个简单的用于测试 lcov 和 gcov 的 C++ 源码示例:
```cpp
// test.cpp
#include <iostream>
int add(int a, int b) {
return a + b;
}
int main() {
int a = 2;
int b = 3;
int c = add(a, b);
std::cout << "The sum of " << a << " and " << b << " is " << c << std::endl;
return 0;
}
```
可以使用以下命令编译和运行示例代码:
```bash
g++ -fprofile-arcs -ftest-coverage test.cpp -o test
./test
```
其中,`-fprofile-arcs` 和 `-ftest-coverage` 参数用于启用代码覆盖率检测,并生成 `.gcno` 文件。执行程序后,会在当前目录下生成 `test.gcno` 文件。
可以使用以下命令运行 `lcov` 工具,生成代码覆盖率报告:
```bash
lcov --directory . --capture --output-file coverage.info
genhtml coverage.info --output-directory coverage-report
```
这将在当前目录下生成 `coverage.info` 文件和 `coverage-report` 目录,其中 `coverage-report/index.html` 文件是代码覆盖率报告的入口。