Where are the gcov symbols?

Solution 1:

I just spent an incredible amount of time debugging a very similar error. Here's what I learned:

  • You have to pass -fprofile-arcs -ftest-coverage when compiling.
  • You have to pass -fprofile-arcs when linking.
  • You can still get weird linker errors when linking. They'll look like this:

    libname.a(objfile.o):(.ctors+0x0): undefined reference to 'global constructors keyed to long_name_of_file_and_function'

This means that gconv's having problem with one of your compiler-generated constructors (in my case, a copy-constructor). Check the function mentioned in the error message, see what kinds of objects it copy-constructs, and see if any of those classes doesn't have a copy constructor. Add one, and the error will go away.

Edit: Whether or not you optimize can also affect this. Try turning on / switching off optimizations if you're having problems with it.

Solution 2:

The flag you're looking for is -lgcov when linking. That is, change:

gcc AllTests.o CuTestTest.o CuTest.o -o TestTest

to

gcc -lgcov AllTests.o CuTestTest.o CuTest.o -o TestTest

Solution 3:

You should be able to specify only --coverage on the command line when compiling and linking.

According to man gcc:

The option is a synonym for -fprofile-arcs -ftest-coverage (when compiling) and -lgcov (when linking).

Solution 4:

I found, as suggested here, that adding -lgcov to the build line when building a shared library that contains .o's built with -fprofile-arcs -ftest-coverage solved this for me. And of course linking the executable with -lgcov. Built the shared library like so:

g++    -shared -o libMyLib.so src_a.o src_b.o src_c.o -lgcov

And the executable like so:

g++ -o myExec myMain.o -lMyLib -lgcov

Adding the -lgov to the build of the shared library (not just the exe), solved this additional error for me:

hidden symbol `__gcov_merge_add' in /usr/lib/gcc/x86_64-redhat-linux/4.1.2/libgcov.a(_gcov_merge_add.o) is referenced by DSO
/usr/bin/ld: final link failed: Nonrepresentable section on output

Please note that -lgcov must be the last linked library.