Search code examples
c++linkerstatic-librariesdynamic-library

Static library & Dynamic library - more C++ fun


Let's say I want to create a dynamic library dynamic.so, but my code references a function that exists in some other, static library static.a. Naturally, if I compile and link with g++ and the -sharedoption, dynamic.so will expect the referenced function to be implemented in some other dynamic library that I would have to link at runtime together with dynamic.so. In other words, it will be happy as long as I do -l static (modulo syntax).

But since that other library is actually a static one, I cannot do that. Let's assume I can not link the static library at compile time of my main program either, but I'm forced to only use dynamic libraries. So what I really want is to include the compiled code off the referenced function from static.a in dynamic.so.

This answer to a related question suggests using the --whole-archive option to include static.a in dynamic.so. However, in my case, static.a is huge. And I really don't need all of it, I only need that one function definition.

This answer to another question explains that linking to a static library at compile time means that only the code that is actually referenced gets included in the binary. Well, this is exactly what I would like to do for my single function reference! I don't really want the whole static archive in there.

But how can I achieve that? How can I include just the required parts of static.a in dynamic.so?


Solution

  • You don't need --whole-archive: just link your dynamic.so like this:

    gcc -shared -fPIC -o dynamic.so $(OBJS) -lstatic
    

    This will pull into dynamic.so anything the linker needs from libstatic.a. See this explanation to understand why that is.

    One problem might be that your libstatic.a is compiled without -fPIC. On 32-bit x86/Linux, that would still work (though your library will not be saving you much RAM if multiple processes are using it). On x86_64, linking non-fPIC code into a shared library can't work, so you'll have to rebuild it with -fPIC.