What really happens inside Hello World?

(Compilers and Linkers in action)

Return to table of contents
 

Compilers

A C-compiler is a fairly dumb piece of software.  Its purposes include: The conversion of keywords into machine code can be thought of as a simple look-up
and replace exercise where C-language constructs are replace by "stock" machine code
fragments (this greatly simplifies the action of a compiler but it is sufficient for our purposes).
The thing to note here is that the compiler only understands C-language keywords and
various directives.  So what actually happens when you compile and link the infamous
"Hello World" program?  Let's see....
 
#include <stdio.h> Line 1
void main()  Line 2
{ Line 3
        printf("Hello World\n"); Line 4
} Line 5

A line-by-line dissection.
Line 1:  This line tells the compiler to glue in the contents of the text file stdio.h which is to be
found in the include directory associated with your compiler.  Most compilers set themselves up
on a computer's disk in a particular way.  C-compilers traditionally set up a directory tree as follows:

The bin directory contains any exectuble files associated with the compiler (such as the compiler program
itself).   The lib directory contains any libraries of functions which are available for use by program you may
develop.  The include directory contains files which include declarations of constant, functions and other items
which facilitate compilation.  One of these files is stdio.h.  This file contains declarations of such functions as
printf, scanf and so on.

Line 2:   This line states that what follows is a function called main.  It takes no parameters and returns nothing.

Line 3: This is simply the opening brace for all of the statements within main.

Line 4: This statement represents a function call to a function called printf.  It is important to note that the
compiler has no built-in knowledge of this function as it is not a C-language keyword.  How does it know what
to do with it then?  How is it to know that you are allowed pass a string as a parameter?  The answer to
both of these questions lies in the include file stdio.h.  This file contains a forward-declaration (or prototype)
of the function printf which informs the compiler of the permissible parameters.  If you had not included the
file stdio.h, the compiler would either signal an error or warning when it encountered printf.

Line 5.  This is the closing brace for the function main.

When the compiler compiles this program, it opens up the stdio.h file and compiles all within it.  Mostly this
process will consist of noting function prototypes and other symbol definitions.  The compiler then proceeds to
compile each line of the Hello World program, converting all relevant  C keywords into equivalent machine code
constucts.  It also builds up a table of symbols which it will pass on the linker.  One of these symbols will be _printf.
 

The Linking process.

The main duty of a program linker is to bind together all of the different sections of machine code which are
needed to make a fully functional program.  In the above example, the linker must bind together the machine
code produced by the compiler when it proecessed the main function, and, it must also bind in the code required
to implement the function printf.  In the case of this example, the linker is passed the output from the compiler -
an object file - and a list of libraries where it may find any additional functions which are called upon by code
in the object file.    There is only one such function in this case: printf.  The linker searches out this code in the
supplied list of libraries (a library is just a collection of pre-compiled functions) and when it finds it, attaches it to the code
from the compiler.  The final executable file produced by the linker will then take the following form:


(By the way, you might be wondering why there is a leading underscore in front of each of the functions names.
These are included because, by convention, C-compilers pre-pend an underscore to all symbols which they
process)