One of our clients had a project that included C for embedded systems, Java, and C++ thrown in (mostly for unit testing). One of the tasks was to investigate code coverage tools for the C portion which led us to gcov (see gcov(1)). During the build for gcov, we encountered the following link errors:
/usr/bin/ld: obj/Test_function: hidden symbol `__gcov_init’ in /usr/lib/gcc/i386-redhat-linux/3.4.5/libgcov.a(_gcov.o) is referenced by DSO
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: ld returned 1 exit status
This occurred when the C++ was being linked to our application’s shared libs.
After some investigation, the solution came via the gcc(1) description for -shared option:
… For predictable results, you must also specify the same
set of options that were used to generate code (-fpic, -fPIC, or
model suboptions)…
The gcov parameters in this case are -fprofile-arcs and -ftest-coverage. These parameters were added with the -fPIC option for gcc -shared for the shared libraries. Then the code was able to link and run properly.