You are on page 1of 4

EMBARCADERO HOME LOCATION | KOREAN |LOG ON

Watch, Follow, &


Connect with Us
Share This

COMMUNITIES ARTICLES BLOGS RESOURCES DOWNLOADS HELP

EDN C++Builder Programming

An Overview of Compiling and Linking RATING


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

An Overview of Compiling and Linking Download Trial


- Ed Mulroy
Compiling Include le Buy Now
DEF le Header le
DLL Your source code can use some functions and variables for which the source code is not in the le.
Free 30-day trial! Develop for
For instance it might use printf to write to the screen. The way this is handled is to have a le or les Windows, Mac, Android, iOS,
Dynamic Linked Library devices and gadgets!
which provide denitions for those other things it might use. For instance, it might provide a function
Dynamic linked
prototype which denes what the function printf is like.
Explicit linked
Header le These les are called header les or include les. The les are text les written in the C or C++
language. The header le or les are bundled into the source code by a statement like this:
Implicit linked
Import library #include <stdio.h>
Include le
Library le When the compiler begins to run it rst runs a preprocessor phase. During that phase the text from the
header les are inserted into its image of your source le at the point where the #include statement is
Linking
found.
Module Denition le
Object le During the next phase, the compiling phase, it sees the result of that preprocessor phase. What was in
the header les appears as if it were part of the source le.
Runtime library
Startup code
Object le
Static library
Compiling
Static linked
A compiler takes your le containing source code and translates it to machine code. It then places
that machine code into an output le called an object le and has the le extension .obj.

Each of the items your les provides or needs has a name, a symbol. The object le 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 le is called compiling.

Linking
After the compiler has created all the object les, another program is called to bundle them into an
executable program le. 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 les you have told it to use.

It assembles two lists,

- the list of publics


Items found in the object les. These are items which are provided by those les C Builder
85k likes
- the list of unresolved externals
Items the object les say they need but do not have

It then examines the object les, nding the publics and placing their addresses into unresolved
externals until all needs are satised. At that point it has what it needs to create a binary le
Like Page
containing the whole program. This binary le is the executable le, the program, and normally has the
le name extension .exe
Be the rst of your friends
Library le to like this
Startup code
Runtime library
Your object les 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 le which provides this code to initialize things and run your program. That le is called the
startup code.
More social media choices:
A library le is a collection of object les with an master list or table of the publics and externals for C++Builder on Google+
each. Its le name has an extension of .lib The linker uses libraries as resources from which it can nd
publics which match the unresolved externals from your object les. It looks in libraries for matching @RADtools on Twitter
public symbols for any unresolved external from your object les.

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 le containing object les that I mentioned above is called a static library. It contains
object les whose code may or may not be bundled into your program depending upon if the items the
object le 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 le 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 le 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 Denition File
DEF File
A module denition le normally has a le name extension of .def It is often called a def le. It is a text
le 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 le 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
denition les are only used when you have special needs.

LATEST COMMENTS

Move mouse over comment to see the full text

Server Response from: ETNASC02

Copyright 1994 - 2013 Embarcadero Technologies, Inc. All rights reserved.. Site Map

You might also like