An Overview of Compiling and Linking

By: Edward Mulroy

Abstract: An overview of the compiling and linking process covering include files, object files, libraries, DLL's, import libraries, static linking, dynamic linking, implicit linking and explicit linking

An Overview of Compiling and Linking
- Ed Mulroy
Compiling
DEF file
DLL
Dynamic Linked Library
Dynamic linked
Explicit linked
Header file
Implicit linked
Import library
Include file
Library file
Linking
Module Definition file
Object file
Runtime library
Startup code
Static library
Static linked
Valid HTML 4.01!
Include file
Header file
Your source code can use some functions and variables for which the source code is not in the file. For instance it might use printf to write to the screen. The way this is handled is to have a file or files which provide definitions for those other things it might use. For instance, it might provide a function prototype which defines what the function printf is like.

These files are called header files or include files. The files are text files written in the C or C++ language. The header file or files are bundled into the source code by a statement like this:

#include <stdio.h>

When the compiler begins to run it first runs a preprocessor phase. During that phase the text from the header files are inserted into its image of your source file at the point where the #include statement is found.

During the next phase, the compiling phase, it sees the result of that preprocessor phase. What was in the header files appears as if it were part of the source file.
 
Object file
Compiling
A compiler takes your file containing source code and translates it to machine code. It then places that machine code into an output file called an object file and has the file extension .obj.

Each of the items your files provides or needs has a name, a symbol. The object file also has lists of those symbols that your code provides and of those symbols which it needs. The process of translating the source code into an object file is called compiling.
 
Linking
After the compiler has created all the object files, another program is called to bundle them into an executable program file. That program is called a linker and the process of bundling them into the executable is called linking.

The linker looks at all the object files you have told it to use.

It assembles two lists,

- the list of publics
Items found in the object files. These are items which are provided by those files

- the list of unresolved externals
Items the object files say they need but do not have

It then examines the object files, finding the publics and placing their addresses into unresolved externals until all needs are satisfied. At that point it has what it needs to create a binary file containing the whole program. This binary file is the executable file, the program, and normally has the file name extension .exe
 
Library file
Startup code
Runtime library
Your object files do not contain all the items that are needed. When Windows loads and starts to run your program some initialization must be done to set up the program to run. The compiler came with an object file which provides this code to initialize things and run your program. That file is called the startup code.

A library file is a collection of object files with an master list or table of the publics and externals for each. Its file name has an extension of .lib The linker uses libraries as resources from which it can find publics which match the unresolved externals from your object files. It looks in libraries for matching public symbols for any unresolved external from your object files.

The default libraries that the linker uses in building your program are called the runtime libraries. They were supplied with the compiler.
 
Static library
Import library
Dynamic Linked Library
DLL
The library file containing object files that I mentioned above is called a static library. It contains object files whose code may or may not be bundled into your program depending upon if the items the object file contains is needed by your code.

There is a form of executable which is not intended as a standalone program but rather has code in it which can be called by functions in an executable. That form of executable is called a Dynamic Link Library or DLL and usually has a file name extension of .dll

The advantage of a DLL is that the inside of it need only be on your disk or in memory once. Many programs may use this code, yet they do not need to have a duplicate copy of the code inside of each of them.

The way this is handled is that the DLL contains a list or table of the symbols for the items it provides. The executable contains a list or table of the symbols and DLL names it needs. When Windows starts the program its program loader looks at the list, loads the DLL into memory and inserts the addresses of the DLL functions that are called into the proper places in the memory image of the program. This process is said to "import" the DLL.

To do this the program must have a list or table of the DLL's it needs and a list or table of the symbols it needs from each DLL. The linker built this list into the program and it got the information from an import library. The import library file name has an extension of .lib, the same extension as is used with a static library. It is a small library and only contains the DLL and symbol information. It does not contain code.
 
Static linked
Dynamic linked
A program which requires a Dynamic Linked Library, a DLL, is said to be Dynamic Linked.

A program which does not require a DLL is said to be Static Linked.

Because all programs running under Windows require DLL's that Windows provides, the statement that a program is dynamic linked or static linked refers to needing DLLs other than the normal ones Windows provides.
 
Implicit linked
Explicit linked
If you wished to do so you could write your code so that you manually loaded a DLL, loaded the address of any function you needed, assigned it to a pointer to function and then called the function in the DLL. You would also have to unload the DLL when you were done with it. When you do that you are said to be Explicitly Linking to the DLL because you have explicit code in your program to do it.

If you use an import library then the Windows program loader did that for you and the program is said to be Implicitly Linked.

It is common for people, even developers with much experience, to use the term "static linked" when what they are speaking of "implicit linked". Be aware of this so as to not get confused when reading their descriptions of things.
 
Module Definition File
DEF File
A module definition file normally has a file name extension of .def It is often called a def file. It is a text file containing items describing how a program should be linked. Along with things such as stack size and other memory arrangements it can also contain descriptions of what DLL's are needed and the exported functions the program uses from each of those DLL's.

The def file used to be the only scheme available for telling the linker about a DLL when implicitly linking. Import libraries are easier to use and are now the common way to do this. These days module definition files are only used when you have special needs.

Server Response from: ETNASC02