> For the complete documentation index, see [llms.txt](https://ref.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ref.gitbook.io/notes/c-c++/gcov.md).

# gcov

Make sure `gcc --version` matches compatible version against `gcov --version` .

**Makefile**

```bash
main: export GCOV_PREFIX=/tmp/cov
main: export GCOV_PREFIX_STRIP=4

main : main.cpp helper/helper.c Makefile
        #g++ -o main main.cpp -fprofile-arcs -ftest-coverage && ./main 123
        g++ -o main main.cpp helper/helper.c -g -O0 -coverage && ./main
        mv *.gcno $(GCOV_PREFIX)
        cp main.cpp $(GCOV_PREFIX)
        mkdir $(GCOV_PREFIX)/helper
        cp helper/helper.c $(GCOV_PREFIX)/helper
        lcov -c --directory $(GCOV_PREFIX) --output-file $(GCOV_PREFIX)/coverage.info
        genhtml $(GCOV_PREFIX)/coverage.info -o $(GCOV_PREFIX)/html/

clean:
        @rm -rf main *.o *.gcda *.gcno *.gcov /tmp/cov/* 2>/dev/null
        git clean -fdx

.PHONY: clean

```

I'm still uncomfortable that I need to `cp main.cpp ...` So the `gcno` and other stuff does not really have the source info. In a huge codebase with subdirs and inter linkage,  the safest I see is to merely use the current directory as output dirs for gcov.&#x20;
