Compiler Steps
Understanding how the C/C++ compiler works will help you better understand some issues you may encounter. Remember that C/C++ compilers are relatively primitive, so you have to be a little careful when working with them.
How the compiler works
The C/C++ compilers work four stages:
- Preprocessing: the code is read from your source code files. This may involve some processing of the text itself, which includes things like ignoring any comments in the code.
- Compiling: the code is then converted into assembly instructions, and an assembly program is output.
- Assembling: the assembly version of the program is converted into machine code, and stored in object files.
- Linking: the compiler uses a linker to join together the machine code from your program with other machine code you have used from the programming libraries. This then outputs an executable program.
We talked briefly about compiling and assembling in Part 0 so lets just quickly look over the two steps new with C/C++ compared to C#.
Pre-processing
In the first step, the compiler will process your code to prepare it for the actual compilation to come. Some parts of the code in your program is written for this preprocessor. The main one is #include
. This is a preprocessor instruction to include the code from another file at this point. In effect, this is like a large copy and paste action.
For #include
to work, the preprocessor needs to know where the included files can be found. There are some default paths that are searched, which includes the current directory. You can also add a -I
option when compiling to add additional paths for the preprocessor to search.
Linking
With C/C++ compilers, the assembler creates what are known as object files. These contain machine code for parts of the program that need to be all connected (linked) together. For example, where you call write_line
, the code for this is in the SplashKit a library. The object files where `write_line“ is used need to know where the SplashKit machine code is so that it can jump to it at run time. The linking process creates these connections.
For the linker to work, it will need to know which libraries you use and where to find them. The -l
option lets you tell the linker which libraries you want linked in, and the -L
option can be used to indicate where those libraries can be found.