|An Overview of Compiling and Linking
- Ed Mulroy
|Dynamic Linked Library|
|Module Definition File|
Your source code can use functions and variables for which you do not provide
source code. For example it might use printf to write to the screen. The
way this is handled is to have another file or files which provide definitions for
items which are provided elsewhere.
Those 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:
When the compiler begins what it first does is to run 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.
The compiler tranlates your source code into machine code, binary instructions
for the computer. It places the machine code into an output file called an
object file. The name of the object file is of the same base name as that
of the source file but with a file extension of .obj
Each of the items your files provides or needs has a name, a symbol. The object file also has lists of the symbols your code provides and of the symbols which it needs. This process of translating the source code into an object file is called compiling.
After the compiler has created all the object files, another program is
called to bundle them into an executable program file. That program is
the linker and the process of bundling them into the executable is called
The linker looks at all the object files you have told it to use and creates 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
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.
|Dynamic Linked Library|
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.
A program which requires a Dynamic Linked Library, a DLL, is said to be
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.
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|
A module definition file normally has a file name extension of .def and 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
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.