How to resolve __gcov_init undefined reference issue when linking
Asked Answered
S

7

61

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?

Spada answered 22/5, 2013 at 2:38 Comment(0)
C
78

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.

Chromatics answered 13/9, 2013 at 4:9 Comment(3)
For me adding --coverage to LFLAGS solved the issue. Earlier I was using only -lgcov for LFLAGS.Seduction
I believe adding --coverage to LFLAGS is only necessary for the LLVM compiler on Mac.Ostrander
Add -lgcov before -lc in the LFLAGSVert
R
17

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.

Rusel answered 31/5, 2013 at 18:20 Comment(3)
Yes, I've also tried -lgcov link option but still encountered above issue 1). Thanks for your replySpada
That was what I missed. Thanks! It worked.Deshawndesi
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".Stereoscope
S
6

you have to provide LDFLAGS to resolve this issue.

LDFLAGS += " -lgcov --coverage"
Seleucia answered 25/2, 2019 at 10:0 Comment(0)
C
1

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.

Counterattraction answered 6/3, 2015 at 18:15 Comment(0)
H
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

Herron answered 21/2, 2019 at 21:4 Comment(0)
D
1

I had undefined reference to gcov functions (undefined reference to '__gcov_exit') while I tried to enable coverage on a C project using a C++ test harness (CppUTest). Build system was handled by CMake.

Compilers and gcov were aligned on the same version (gcc --version, g++ --version and gcov --version gave the same version) but it seems that my build system was previously configured to use gcc 5 while g++ 8 and gcov 8 were used (resulting to an additional included directory by the linker: usr/lib/gcc/x86_64-linux-gnu/5). I cleaned the build tree and generated it again thanks to CMake which fixed the error.

Disputation answered 10/11, 2021 at 16:56 Comment(0)
G
1

I have seen this issue too and as most of the answers above indicated it needed us to add lcov/gcov libraries at the time of linking. We are using cmake and in CmakeLists.txt file we were missing

target_link_libraries(${TARGET_NAME} PRIVATE gcov)

This was needed of course in addition to the build flag "--coverage" (Pls note we can either use "--coverage" or "-fprofile-arcs -ftest-coverage" separately)

Gilmer answered 20/12, 2021 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.