Can I create a gcc linker for a static library?

I have a library consisting of 300 C ++ files.

A program that consumes a library does not want to dynamically reference it. (For various reasons, but the best thing is that some supported platforms do not support dynamic linking)

Then I use g ++ and ar to create a static library (.a), this file contains all the characters of all these files, including those that the library does not want to export.

I suspect that linking the consumer program to this library takes too much time, because all .o files inside .a must still have their own links, and the linker has more characters to process.

When creating a dynamic library (.dylib / .so), you can actually use a linker that can resolve all the symbols inside lib and export only those that the library wants to export. However, the result can only be β€œtied” to the consumption program at run time.

I would like to somehow get the benefits of dynamic linking, but use a static library.

If my Google searches are correct, if you think this is really impossible, I would like to understand why this is not possible, since it looks like it can win many C and C ++ programs.

+4
source share
2 answers

I have not tried or tested this, but it seems that ld ability to perform incremental or partial binding may be what you are looking for. Check if there is an option --relocatable (maybe you need to look at the -Ur option if it deals with C ++), when applied to object files that will be included in the library, it will do what you want.

I think that then you can use the output of this operation as an object file (or have it in a static library) for the final stage of your program.

+1
source

Static libraries are just archives (hence the ".a") - a collection of .o files. Like the tar archive, just simpler. Since ar is not a linker, conglomeration (as "ld -r" did) and therefore the exception of the intra-library character are not eliminated.

This is why shared libraries were invented in the first place, and now they are quite common, so people simply ignore the disadvantages of static libraries. They just go "compiles" it. "

+4
source

Source: https://habr.com/ru/post/1333311/


All Articles