Statically linking against a shared library

Thorsten Glaser tg@66h.42h.de
Sat Mar 12 22:11:00 GMT 2005


Hello!

I'd like to ask if someone (besides me) has ever wished to link in
a shared library statically.

The most common use case would be you've got a shared library
(no matter whether it's .DLL, .so or .dylib) whose static
counterpart you don't have or won't get (for some reason, which
to discuss is not the task here), and you'd like to link a
programme against it, in order to distribute the programme, but
not the shared library.

Bonus points for being able to strip out unused material, but
I guess, from what little I know about shared libraries, that
it's impossible on all arches and object formats.

But hey, sometimes, the pros outweigh the cons, and if someone
wants to do this, he'd better be prepared that the resulting
executable's size is only the size of the dynamic library +
executable minus one file header (and probably a bunch of padding).

After all, ld.so can do it for me, so why can't ld?
(Or did I just not RTFM enough?)


tg@odem:/home/tg $ cat x.c
#include <stdio.h>

void foo(void);

void
foo(void)
{
        printf("Hello ");
}
tg@odem:/home/tg $ cat y.c
#include <stdio.h>

extern void foo(void);

int main(void);

int
main(void)
{
        foo();
        printf("World\n");

        return 0;
}
tg@odem:/home/tg $ gcc -shared -o libfoo.so x.c
tg@odem:/home/tg $ gcc -o y y.c libfoo.so
tg@odem:/home/tg $ objdump -p y | fgrep NEEDED
  NEEDED      libfoo.so
  NEEDED      libc.so.35.1
tg@odem:/home/tg $ gcc -o y y.c -static libfoo.so
/usr/bin/ld: attempted static link of dynamic object `libfoo.so'
collect2: ld returned 1 exit status

Oh, btw: could that ` be changed to ' please? Markus Kuhn seems
to have had success in persuading the GCC people, so...

Thanks in advance,
//mirabile
-- 
> [...] Echtzeit hat weniger mit "Speed"[...] zu tun, sondern damit, daß der
> richtige Prozeß voraussagbar rechtzeitig sein Zeitscheibchen bekommt.
Wir haben uns[...] geeinigt, dass das verwendete Echtzeit-Betriebssystem[...]
weil selbst einfachste Operationen *echt* *Zeit* brauchen.	(aus d.a.s.r)



More information about the Binutils mailing list