Search code examples
linuxstatic-librariesld

Automatic selection of .o file in static library (.a file)


I'm developing a static library (libfb.a) file which contains multiple implementations of two functions:

  • foo.c contains void foo() { ... }.
  • bar.c contains void bar() { ... }.
  • foobar.c contains void foo() { ... } and void bar() { ... }.

All the 3 implementations (...) are different, because if both foo and bar are needed, both of them can be implemented more efficiently, using each other.

How can I combine the .o files to an .a file for which GNU ld would do automatic selection, i.e. gcc prog.c libfb.a will

  • use foo.o within libfb.a if prog.c calls foo, but not bar;
  • use bar.o within libfb.a if prog.c calls bar, but not foo;
  • use foobar.o with libfb.a if prog.c calls both foo and bar.

I was trying to do it using weak symbols and weak aliases, but it didn't work. Maybe it's not possible. Any ideas how it can be done?


Solution

  • Repeating @A.Monti's comment as an answer.

    It's not possible. You can't detect when prog.c calls both foo and bar using the available primitives.