47

I now work on C code coverage study and encountered following issue, GCC version 4.4.6:

  1. Added compiler flag CFLAGS = --coverage and linker option LDFLAGS := --coverage or LOCAL_LDLIBS := --coverage and got the error:

undefined reference to '__gcov_init'" and "undefined reference to '__gcov_merge_add'

  1. Added option LOCAL_LDFLAGS := --coverage, and got link error:

libgcov.a(_gcov.o): in function __gcov_set_sampling_rate: undefined reference to '__gcov_sampling_rate' libgcov.a(_gcov.o): in function gcov_exit: undefined reference to '__gcov_pmu_profile_filename' libgcov.a(_gcov.o): in function __gcov_init: undefined reference to '__gcov_pmu_profile_options' '__gcov_pmu_top_n_address'

Can anyone help to provide some suggestions on this issue?

lilingmzai
  • 491
  • 1
  • 4
  • 7

5 Answers5

60

Try this approach:

Compile the code for which you want to generate the coverage with these options:

CFLAGS: -fprofile-arcs -ftest-coverage

LFLAGS: -lgcov --coverage

If this doesn't solve the problem, then please provide some information on the structure of your application, i.e. whether its single program or an application involving shared/static libraries etc.

Hope that helps!

Michael Allen
  • 5,433
  • 3
  • 35
  • 59
Rajen
  • 781
  • 5
  • 7
12

Are you linking with -lgcov?

If you are using a Makefile it would be of great help to have a look at it in order to help you.

Gabriel Staples
  • 11,777
  • 3
  • 74
  • 108
niglesias
  • 381
  • 7
  • 15
  • Yes, I've also tried -lgcov link option but still encountered above issue 1). Thanks for your reply – lilingmzai Jul 02 '13 at 07:43
  • That was what I missed. Thanks! It worked. – Spixmaster Mar 14 '21 at 11:29
  • Perfect! I had a custom bash build script with `gcc ${OBJECT_FILES[@]} -o "$BUILD_DIR/$PROGRAM_NAME"` in it for the linking portion of the build. Simply adding `-lgcov` like this made it work!: `gcc ${OBJECT_FILES[@]} -lgcov -o "$BUILD_DIR/$PROGRAM_NAME"`. – Gabriel Staples May 30 '21 at 05:06
4

you have to provide LDFLAGS to resolve this issue.

LDFLAGS += " -lgcov --coverage"
Ajay.kundu
  • 175
  • 10
1

I found I had to put the '-lgcov' to the right of the object being profiled instead of in Flags. Something like. gcc -pg -o myprog myprog.o -lgmp.a -lgcov

0

I can't be sure which change finally did the trick for me but I think it was the -fprofile-generate flag. Using GNAT GPS I went to the Switches tab on the left and then selected the Ada Linker tab on the top. Then I enabled the checkbox for Code Coverage. Oh yeah I've found that on the Builder tab in that same area if you enable the Recompile if switches changed checkbox it can save a lot of teeth-gnashing. Probably slows things down for the pros but I found it helpful.

Tod
  • 7,806
  • 5
  • 46
  • 85