
Compiling process in C programming
Projects » Embedded system » Compiling process in C programming
- January 10, 2019
- , 3:01 pm
- , Embedded system
Foreword
We usually write our embedded system with C language. However, C is still a high level language and we will need a compiler to generate it to executable code that can run on our system. Today we will see how compiler can do this.
Now, let’s get started.
All compilation process
Here are the full step of compilation process
Step 1: Pre-processing
The pre-processing step will take source file (.c file) and generate to .i file
In the pre-processing step, the compiler will do 3 things:
- Expand header files.
- Expand macros and inline functions.
- Remove all comments
Let’s take a look at how .i file looks like after doing pre-processing step in figure 2
Figure 2: Pre-processing step
Step 2: Compiling
The compiling step will take .i file and generate to assembly code (.s file), which is an intermediate human readable language.
The .s file will have something like in figure 3
Figure 3: Compiling step (.s file)
Step 3: Assembly
The assembly step will take assembly code (.s file) and generate to object code (.o or .obj file)
.o file will be something like figure 4 if open with a hex editor
Figure 4: Assembly step (.o file)
Step 4: Linking
In a project with several modules, we will have several object files after step 3. In order to make an executable program, all of these files have to be rearranged and all the missing instructions (if you are using libraries) must be linked together. That’s why this process is called linking.
Step 5: Loading
After linking, we will have only 1 executable file (the name is a.out if we compile without any options) which can be run on our target controller.
Compile yourself a simple program and run
// Program to multiply 2 numbers #include <stdio.h> #define MUL(a,b) (a*b) int main(void) { int a = 5; int b = 10; printf("Result: %d\n", MUL(a,b)); return 0; }Run this command in terminal, it will create .i, .s, .o and .out file from your source file
gcc –Wall –save-temps main.cLet’s run your executable file by run this command in terminal
./a.outThe result which is displayed on terminal should be 50