Static linking is the linking of a calling program and
one or more called programs into a single executable module.
When the program is loaded, the operating system places
into memory a single file that contains the executable code and data.
The primary advantage of static linking is that you can use it to
create self-contained, independent executable modules.
Static linking has these disadvantages, however:
- Because external programs are built into the executable
file, the executable file increases in size.
- You cannot change the behavior of the executable file without
recompiling and relinking it.
- If more than one calling program needs
to access the called programs, duplicate copies of
the called programs must be loaded in memory.
To overcome these disadvantages, you can use shared libraries:
- You can build one or more subprograms into a shared library; and
several programs can call the subprograms that are in the shared library.
Because the shared library code is separate from that of the calling programs,
the calling programs can be smaller.
- You can change the subprograms that are in the shared library
without having to recompile or relink the calling programs.
- Only a single copy of the shared library needs to be in memory.
Shared libraries typically provide
common functions that can be used by a number of programs.
For example, you can use shared libraries to implement subprogram
packages, subsystems, and interfaces in other programs or to create
object-oriented class libraries.
Example: creating a sample shared library