Discussions on C / C ++ Static Libraries

I never thought about the problem below, but since now I have to deal with a bunch of dependencies in my code, I thought it was better to get my facts straight. Let's limit this to a modern version of Linux, say ubuntu amd64.

Since static libraries do not contain links to a dynamic library, how are undefined characters allowed in static libraries? Can the dependent binary dynamic character loading undefined, or should the characters be resolved by another static library or object file at compile time?

Can the compiler resolve dependencies (applications depending on the static library) by linking to a dynamic library, and if so, is the code text statically resolved to the resulting binary file, or will there be a dynamic link?

For example, the L static library uses malloc from libc6.so and will be used by application A Will L and A use dynamically malloc from libc6.so?

+4
source share
2 answers

A static library is nothing more than a list of object files archived together.

How undefined characters are allowed in static libraries?

They are not allowed because there are no undefined characters. The symbol is considered undefined only at the stage of coupling, and when creating a static library there is no connection.

When linking a binary to a static library, there may be undefined characters. In this case, the static library is processed in the same way as part of your program, and therefore all references to the symbols used in this static library should be available in the amount of the program you are creating. Say, if program A links to the static library B , which uses the character C from some other library D , then program A must link to B and D

can dependent binary dynamically load undefined characters

Yes maybe. But you should not go this route unless you really need a lazy, dynamic resolution.

or must be allowed by symbols with another static library or object file at compile time

Object files as well as static libraries do not allow any characters. This is done by the linker.

Can the compiler decide ...

The compiler does not resolve any dependencies. This is the job of the linker. Dependencies can be resolved at binding time or at runtime by the dynamic linker.

dependencies (application dependencies on a static library) by linking to a dynamic library, ...

Linkers can understand that the static library you use depends on the characters you use in the dynamic library.

and if so, is the code text statically resolved to the resulting binary, or will there be a dynamic link?

If you link to a shared library, nothing will be available in your program. This is the point of shared libraries. The exception is only LTO. As for the static library you are linking to, nothing from this static library will be available dynamically, it will be compiled, and those characters that are not used will be deleted.

For example, the L static library uses malloc from libc6.so, and it will be used by application A. Are L and A using malloc from libc6.so dynamically?

Yes, if the definition of malloc() not available at the time of compiling the static library and the compiler, for some reason it simply enclosed the body of malloc() in the code of the static library. But with malloc() this will not happen. Perhaps this could happen with other functions.

+7
source

static library (file foo.a ) - for practical purposes - a set of object files ( xo , yo , ...), packed with ar archive.

This static library is used at connection time to satisfy undefined links otherwise. Everything that is not executed during the connection (compilation time is not important here) should be performed at boot time through the dynamic library ( bar.so files).

In the last example, with static libary L and program A, both (A, L) will use the same malloc from libc6.so, since you are not using preload, built-in mallocs from possibly different header files, or other tricks, which you did not mention.

+1
source

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