Where are the gcov characters?

I am trying to compile a simple application with gcov and get the following link errors:

gcc AllTests.o CuTestTest.o CuTest.o -o TestTest AllTests.o: In function `global constructors keyed to 0_RunAllTests': /home/p7539c/cutest/AllTests.c:26: undefined reference to `__gcov_init' AllTests.o:(.data+0x44): undefined reference to `__gcov_merge_add' CuTestTest.o: In function `global constructors keyed to 0_TestCuStringNew': /home/p7539c/cutest/CuTestTest.c:30: undefined reference to `__gcov_init' CuTestTest.o:(.data+0x64): undefined reference to `__gcov_merge_add' CuTest.o: In function `global constructors keyed to 0_CuStrAlloc': /home/p7539c/cutest/CuTest.c:379: undefined reference to `__gcov_init' CuTest.o:(.data+0x184): undefined reference to `__gcov_merge_add' collect2: ld returned 1 exit status make: *** [TestTest] Error 1 

I can't seem to find the location of the missing characters. gcov is present on a machine running gcc version 4.1.2

Any ideas? Thank.

In the editor:

Everything seems to work just fine when using gcov with an application that consists of a single .c file. When I have multiple .c files (hence multiple .o files), I have a problem.

The compilation steps are as follows:

 cc -fprofile-arcs -ftest-coverage -g -c -o AllTests.o AllTests.c cc -fprofile-arcs -ftest-coverage -g -c -o CuTestTest.o CuTestTest.c cc -fprofile-arcs -ftest-coverage -g -c -o CuTest.o CuTest.c 
+50
gcc profile gcov
Feb 19 '09 at 18:06
source share
8 answers

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

  • At compilation, you must pass -fprofile-arcs -ftest-coverage .
  • When binding, you must pass -fprofile-arcs .
  • When linking, strange linker errors still occur. They will 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 has a problem with one of your constructor-created constructors (in my case, a copy constructor). Check the function indicated in the error message, see what objects it copies and constructs, and see if any of these classes have a copy constructor. Add one and the error will disappear.

Edit: modify or not optimize. Try turning on / off the optimization if you have problems with it.

+68
May 18 '09 at 19:33
source share

The flag you are 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 
+13
Feb 19 '09 at 21:38
source share

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

According to man gcc :

This option is synonymous with -fprofile-arcs -ftest-coverage (when compiling) and -lgcov (when linking).

+11
Mar 18 2018-11-11T00:
source share

I found as suggested here that adding -lgcov to the assembly line when creating a shared library containing .o created using -fprofile -arcs -ftest-coverage solved this for me. And of course, linking the executable with -lgcov. Built a shared library as follows:

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

And the executable is like this:

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

Adding -lgov to the build of the shared library (and not just 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 

Note that -lgcov should be the last linked library.

+7
Nov 03 '10 at 13:36
source share

I tried a simple test file with gcc -ftest-coverage -fprofile-arcs test.c and had no problems as you describe.

I suspect gcc is contributing to the gcov library if the -ftest-coverage flag exists when it is linked. Try passing this flag on your gcc command line.

+5
Feb 19 '09 at 19:57
source share

Perhaps it is obvious that this exact error message occurs when binding to a non-gcc linker. We see this error when binding with ifort (because our code includes both Fortran and C modules). Switching to the gcc connection did the trick.

+2
Aug 08 2018-12-21T00:
source share

Great Max Libert, Basically when using autoconf add _LDADD = -lgcov ...

This will solve the problem.

+1
Feb 21 2018-11-21T00:
source share

So, I added - shared with CFLAGS, and now it works with several files. Of course, this sounds in a strange place, so I don’t know what that means.

-one
Feb 19 '09 at 21:18
source share



All Articles