Where are the gcov symbols?

折月煮酒 提交于 2019-11-28 15:52:36
slicedlime

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.

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
Ogre Psalm33

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.

Oskari Timperi

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).

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

I suspect that gcc brings in the gcov library if the -ftest-coverage flag is there when it is linking. Try passing that flag on your gcc command line.

Perhaps obviously, this exact error message is produced when linking with a non-gcc linker. We seeing this error when linking with ifort (because our code includes both Fortran and C modules). Switching to linking with gcc did the trick.

Great Max Lybbert, Basically in case of autoconf usage add _LDADD = -lgcov ...

This will solve the issue.

So I added -shared to the CFLAGS, and now it seems to work with multiple files. Of course, it's coring in a strange place, so I don't know what that's about yet.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!