Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc static library linking vs dynamic linking

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?

like image 309
bala1486 Avatar asked Oct 24 '25 00:10

bala1486


1 Answers

Why is it giving error when linked statically with libcunit.a

The problem is that your libcunit.a was built on an ancient Linux system, and depends on symbols which have been removed from libc (these symbols were used in glibc-2.2, and were removed from glibc-2.3 over 10 years ago). More exactly, these symbols have been hidden. They are made available for dynamic linking to old binaries (such as libcunit.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:

readelf -Ws /lib/x86_64-linux-gnu/libc.so.6 | egrep '\W__ctype_b\W'
   769: 00000000003b9130     8 OBJECT  GLOBAL DEFAULT   31 __ctype_b@GLIBC_2.2.5

readelf -Ws /usr/lib/x86_64-linux-gnu/libc.a | egrep '\W__ctype_b\W'
# no output
like image 119
Employed Russian Avatar answered Oct 26 '25 14:10

Employed Russian