質問

I've never thought about the issue below but since I am now having to deal with a bunch of dependencies in my code I thought I better get my facts straight. Lets restrict this to a modern Linux version say ubuntu amd64.

Since static libraries do not contain dynamic library references, How are undefined symbols resolved in static libraries ? can a depending binary dynamically load the undefined symbols, or must the symbols be resolved by another static library or object file at compile time ?

May the compiler resolve the dependencies (of an application depending on a static library) by linking against a dynamic library, and if so will the code text by statically resolved into the resulting binary or will a dynamic reference exist ?

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

役に立ちましたか?

解決

Static library is nothing but a list of object files archived together.

How are undefined symbols resolved in static libraries?

They are not resolved, because there are no undefined symbols. Symbol is considered undefined only on linkage stage, and there is no linkage going on when you create a static library.

There could be undefined symbols when you link a binary against a static library. In that case, static library is treated just like a part of your program, and thus all references to symbols used in that static library must be available in the scope of the program you build. Say, if program A links with a static library B that uses symbol C from some other library D, then program A must link with B and D.

can a depending binary dynamically load the undefined symbols

Yes it can. But you should not go that way unless you really need lazy, dynamic resolution.

or must the symbols be resolved by another static library or object file at compile time

Object files, as well as static libraries, do not resolve any symbols. It is the linker that does it.

May the compiler resolve ...

Compiler does not resolve any dependencies. It is a linker's job. Dependencies could be resolved at the linkage time, or during a run-time by dynamic linker.

the dependencies (of an application depending on a static library) by linking against a dynamic library, ...

Linkers can understand that static library that you are using depends on symbols that are in dynamic library that you link against.

and if so will the code text by statically resolved into the resulting binary or will a dynamic reference exist ?

If you link against a shared library, nothing from there will be statically available in your program. That's the point of shared libraries. The exception is only LTO. As for the static library that you link with, nothing from that static library will be available dynamically, it is compiled in, and those symbols that are not used are eliminated.

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

Yes, unless the definition of malloc() was available at the time of compiling a static library and compiler for some reason just inlined the body of the malloc() into the code of the static library. But with malloc() it won't happen. Could happed with other functions though.

他のヒント

a static library (a foo.a file) is - for practically purposes - a bunch of object files (x.o, y.o, ...) packed with ar, the archiver.

This static library is used at link time, to satisfy the otherwise undefined references. Everything that is not satisfied at link time (compile time is not important here), must be satisfied at load time, through a dynamic library (the bar.so files).

In you last example, with the static libary L and the program A, both (A, L) will use the same malloc from libc6.so, as far as you don't use preloading, inlined mallocs from maybe different headerfiles or other tricks, that you have not mentioned.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top