Static Versus Dynamic Linking

Most programmers are familiar with static linking; an application calls a routine or procedure whose code is not found in the application's source file. The routine is external to your source file and is declared as such. When the source file is compiled, the compiler places an external reference for the routine in the application's object (.OBJ) file. To create the executable file (.EXE) for the application, the application's object file is linked with an object file that contains the code for the routine. The result is an EXE file that contains the application code, as well as a copy of the code for the routine. The following figure illustrates the process of building a statically linked application.

   My_Application.OBJ
  ┌────────────────────────┐
  │ EXTERNAL               │
  │     Your_Routine       │
  ├────────────────────────┤
  │ .                      │                  My_Application.EXE
  │ .                      │                 ┌──────────────────┐
  │ CALL ???; Your_Routine ├──┐              │ .                │
  │                        │  │  ┌──────┐    │ .                │
  └────────────────────────┘  ├─│ LINK ├───│ CALL xxx         │
                              │  └──────┘    │ .                │
   Your_Library.OBJ           │              │                  │
  ┌────────────────────────┐  │         xxx: │ Your_Routine:    │
  │ PUBLIC                 │  │              │                  │
  │     Your_Routine       │  │              └──────────────────┘
  ├────────────────────────┤  │
  │                        │  │
  │ Your_Routine:          │  │
  │                        ├──┘
  └────────────────────────┘

Static Linking

When OS/2 loads a statically linked program, all the code and data are contained in a single EXE file and the system can load it into memory all at once.

The advantages and disadvantages of static linking are summarized in the following table.

┌──────────────────────────────┬──────────────────────────────┐
│Advantages                    │Disadvantages                 │
├──────────────────────────────┼──────────────────────────────┤
│Compile in pieces             │External routines built into  │
│                              │EXE (making EXEs larger)      │
├──────────────────────────────┼──────────────────────────────┤
│Can create libraries of       │EXE cannot be changed without │
│routines that can be linked   │relinking.                    │
│with applications.            │                              │
├──────────────────────────────┼──────────────────────────────┤
│                              │External routines cannot be   │
│                              │shared (duplicate copies of   │
│                              │libraries).                   │
└──────────────────────────────┴──────────────────────────────┘

Dynamic linking permits several applications to use a single copy of an executable module. The executable module is completely separate from the applications that use it. Several functions can be built into a DLL, and applications can use these functions as though they were part of the application's executable code. You can change the dynamically-linked functions without recompiling or relinking the application.

The advantages of dynamic linking are:

DLLs can be used to implement subroutine packages, subsystems, and interfaces to other processes. In OS/2:

OS/2 provides two varieties of dynamic linking: load-time and run-time. In load-time dynamic linking, references are resolved when an application is loaded. In run-time dynamic linking, references are resolved when the application runs.


[Back: Dynamic Linking]
[Next: Load-Time Dynamic Linking]