My build environment is CentOS 5. I have a third party library called libcunit. I installed it with autotools and it generates both libcunit.a
and libcunit.so
. I have my own application that links with a bunch of shared libraries. libcunit.a
is in the current directory and libcunit.so
and other shared libraries are in /usr/local/lib/
. When I compile like:
gcc -o test test.c -L. libcunit.a -L/usr/local/lib -labc -lyz
I get a linkage error:
libcunit.a(Util.o): In function `CU_trim_left':
Util.c:(.text+0x346): undefined reference to `__ctype_b'
libcunit.a(Util.o): In function `CU_trim_right':
Util.c:(.text+0x3fd): undefined reference to `__ctype_b'
But when I compile with .so
like:
gcc -o test test.c -L/usr/local/lib -lcunit -labc -lyz
it compiles fine and runs fine too.
Why is it giving error when linked statically with libcunit.a
?
Didn't notice that the
libcunit.a
is actually found in your case and the problem with linakge is rather in the CUnit library itself. Employed Russian is absolutely right, and he's not talking about precompiled binary here. We understand that you've built it yourself. However, CUinit itself seems to be relying on the symbol fromglibc
which is not available for static linking anymore. As a result you have only 2 options:Nevertheless, my recommendation about your style of static linkage still applies.
-L.
is in general bad practice. CUnit is a 3rd party library and should not be placed into the directory containing source files of your project. It should be rather installed in the same way as the dynamic version, i.e. like you havelibcunit.so
in/usr/local/lib
. If you'd supplyprefix
to Autotools on the configure stage of CUnit, then Autotools would install everything properly. So if you want to link statically with CUnit, consider doing it in the following form:The problem is that your
libcunit.a
was built on an ancient Linux system, and depends on symbols which have been removed fromlibc
(these symbols were used inglibc-2.2
, and were removed fromglibc-2.3
over 10 years ago). More exactly, these symbols have beenhidden
. They are made available for dynamic linking to old binaries (such aslibcunit.so
) but no new code can statically link to them (you can't create a new executable or shared library that references them).You can observe this like so: