How do I enable coverage reports with GCC?

To enable coverage reports with GCC (GNU Compiler Collection), you'll need to use the `-fprofile-arcs` and `-ftest-coverage` options when compiling your code. These options generate additional information necessary for coverage analysis.

Here's a step-by-step guide:

  1. Compile your source file with the coverage options:
  2. gcc -fprofile-arcs -ftest-coverage -o my_program my_program.c
  3. Run your compiled program to generate the execution data:
  4. ./my_program
  5. Compile again, linking against the test coverage libraries (optional for more detailed coverage):
  6. gcc -fprofile-arcs -ftest-coverage -o my_program my_program.c -lgcov
  7. Generate the coverage report using gcov:
  8. gcov my_program.c
  9. Open the generated `my_program.c.gcov` file to view the coverage information.

For HTML reports, you may need to use tools like `lcov` to convert gcov output into a more readable format.


gcc coverage gcov profiling test coverage C++