Search code examples
c++gccmingwinterpreter

Is it possible to see which lines were executed after a command-line app was run?


I am using MinGW (GCC) as a C++ compiler within my application. I have set it to redirect the output of its command line process to my app. Now, suppose I have the following simple C++ code:

int n = 5;
if (n == 6) cout << "YES";
else cout << "NO";

Is there a way to tell what line(s) of code were actually hit during execution of the application? Is there a command I can send to MinGW (GCC) process which, for the given example, would output 1 and 3, as those were the lines hit. And also, in case of a line inside a "for" loop, to tell how many times that statement was actually hit?

And, if not possible, what would be the best approach to having this information? Developing my own compiler or...? Thanks in advance

EDIT: Can someone provide a snippet of commands (in Windows) to be used in order to create a coverage-enabled GCC exe file?


Solution

  • "Is there a way to tell what line(s) of code were actually hit during execution of the application?"

    Yes. It's an intrinsic GCC feature. You'll need to compile and link your code with the --coverage, -lgcov or -fprofile-arcs options set.

    The gcov tool can be used to consolidate and interpret the actual informations gathered during program runs, that were instrumented with --coverage.

    A very good tool to produce browsable consolidated and fairly visualized covearage information from gcov outputs is lcov.