Texas Instruments Interview Question
Software Engineer / DevelopersWhen an executable is produced using static linking, all of the needed machine code from the library is copied into the executable file when the link command is run. For example, suppose prog.c is the source code for a simple C program that makes calls to scanf and printf. You compile the program with the command
gccw -c prog.c
and then you link it with the command
gccw -o prog prog.o
Under static linking, the complete machine code translations of scanf and printf are incorporated into the executable file prog.
When dynamic linking is used to create an executable, machine code from the library is not copied into the executable file. Instead, a little bit of information about where to find the library machine code is put in the executable file. When the executable is actually run, the operating system finds the needed library machine code and loads it into computer memory along with the machine code from the executable. Returning to our example, if the sequence of commands
gccw -c prog.c
gccw -o prog prog.o
is executed in an environment with dynamic linking, the file prog will contain the machine code translation of prog.c and information about where to find machine code for functions like scanf and printf, but it won't contain the machine code translations of scanf and printf.
Dynamic linking has many advantages over static linking. Here are two:
Reduced use of disk space. Dynamically linked executables tend to be much smaller than statically linked executables.
More efficient use of memory at run time. Dynamically linked executables allow the operating system to play clever tricks that may reduce total memory use when many programs are running simultaneously. (Ask me in a lab period if you would like a less vague explanation.)
Dynamically linked libraries can be shared among different processes where as static linked libraries can not be shared and if 2 processes have static linked libraries same part of library should be loaded twice in memory. Where as dynamic linked library allows to share the code of dynamic linked library among processes
static linking--in this case we can use the individual files as independent binary files thus the executable file formed is much larger in size but does not depend on the shared libraries
dynamic linking--here the executable file depends heavily on the shared libraries thus,we cannot directly copy paste and use such binary files.Version incompatibility might also be a problem in the case when you copy and paste such a binary
Static linking is carried out only once to produce an executable,and if several processes call the same object module then several copies exist in the memory.
- Anonymous August 22, 2010Dynamic linking : Allows a process to add, replace, remove object module during execution, and if several processes call same object module then there is only 1 shared copy between several process in memory.