C/C++ Programming 1  /  C/C++ Programming 2
Ray Mitchell

What are Compiling and Linking?

The creation of a working executable program from the original source code file(s) consists of several steps.  Which steps are carried out automatically and transparently to the programmer are dependent upon both the compiler being used and how it is configured.  There are typically 4 major steps: 

    1.  Preprocessing:

During preprocessing a temporary intermediate file is produced which contains the same source code as the original .c (C) or .cpp (C++) file but with all preprocessor directives executed.  That is, all files included using the #include directive will be substituted, all macros defined using the #define directive will be expanded and substituted, etc.  Most compilers have the option of creating a permanent text copy of this intermediate file for debugging purposes.  Such a file will typically have a .i extension and should never be used to produce the finished executable program.

    2.  Compiling: 

During this step the preprocessed code is used to produce a temporary intermediate file containing assembly language.  Assembly language consists of low-level instructions in the form of mnemonics, labels, register names, and constant numeric values which are peculiar to the target processor.  Most compilers have the option of creating a permanent text copy of this intermediate file for debugging purposes.  Such a file will typically have a .a or .asm extension and may be used to produce the finished executable program.

    3.  Assembling:

During assembly the assembly language code is used to produce a permanent intermediate binary file containing object code.  Object code consists of machine instructions, symbol information, relocation information, etc.  Object files will typically will have .o or .obj extensions and are used to produce the finished executable program.

    4.  Linking:

During this final stage of program generation object file(s) are gathered and processed and a permanent executable binary file is created.  It contains both data and machine instructions for direct processor execution.  Executable files will typically will have .exe extensions on DOS based systems and the name a.out on UNIX based systems.