Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do static libraries do linking to dependencies?

Say I have libA. It depends on, for example, libSomething for the simple fact that a non-inline method of libA makes a call to a method in libSomething.h. How does the dependency link up in this case? Does libA have to statically link to libSomething when it is compiled, or will a user of libA (an application using libA) need to link to both libA and libSomething?

Thanks

like image 262
jmasterx Avatar asked Oct 20 '11 20:10

jmasterx


People also ask

How are static libraries linked?

Static libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time.

What happens when you link a static library?

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory.

How do static libraries work?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.

What does static linking do?

Static linking is the result of the linker copying all library routines used in the program into the executable image. This may require more disk space and memory than dynamic linking, but is both faster and more portable, since it does not require the presence of the library on the system where it is run.


3 Answers

Static linking is just copying the whole items (functions, constants, etc) into the resulting executable. If a static library's code contains references to some shared library items, these references will become dependencies in the resulting executable. The same holds if you link a library instead of executable.

This thread discusses how it happens in Linux.

like image 151
vines Avatar answered Oct 17 '22 15:10

vines


A static library is more or less a simple archive of unlinked binary object files (*.o or *.obj), when compiling the archive, no checking is done for dependencies. When one linked one's binary executable (or shared library / DLL) the linker checks for all the necessary dependencies and only then will alert you to any issues.

like image 29
doron Avatar answered Oct 17 '22 15:10

doron


During the build process the compiler translates code into a temporary format, let's call it an object file. In the object file, there is a list of symbols that the compiler could not resolve, usually definitions elsewhere. The linking phase is in charge of resolving these symbols.

The build process feeds files to the linker until all symbols are resolved. There is no physical dependency lists, just lists of symbols to resolve. This allows for symbols to be resolved by using different libraries. For example, one may want to use a Windows library for Windows specific issues; a linux library for linux specific issues. This does not explicitly state that a program is dependent on a Windows library; it could also be dependent on the Linux one.

Some compilers can generate dependency lists, usually for usage in a build process. However, the ultimate responsibility is up to the programmer.

like image 31
Thomas Matthews Avatar answered Oct 17 '22 15:10

Thomas Matthews