I have a library consisting of some 300 c++ files.
The program that consumes the library does not want to dynamically link to it. (For various reasons, but the best one is that some of the supported platforms do not support dynamic linking)
Then I use g++ and ar to create a static library (.a), this file contains all symbols of all those files, including ones that the library doesn’t want to export.
I suspect linking the consuming program with this library takes an unnecessary long time, as all the .o files inside the .a still need to have their references resolved, and the linker has more symbols to process.
When creating a dynamic library (.dylib / .so) you can actually use a linker, which can resolve all intra-lib symbols, and export only those that the library wants to export. The result however can only be “linked” into the consuming program at runtime.
I would like to somehow get the benefits of dynamic linking, but use a static library.
If my google searches are correct in thinking this is indeed not possible, I would love to understand why this is not possible, as it seems like something that many c and c++ programs could benefit from.
I haven’t tried or tested this, but it looks like
ld‘s ability to perform incremental or partial linking might be what you’re looking for. Check if the--relocatableoption (you might also need to look at the-Uroption if dealing with C++) when applied to the object files that would go into the library will do what you want.I think you should then be able to use the output of that operation as an object file (or have it in a static library itself) for your program’s final link step.