Compilation

!Pasted image 20230323150536.png Private or Broken Links
The page you're looking for is either not available or private!

Steps of compilation

  1. Preprocessing Phase Programmers will add a # to certain commands within the source, which direct the preprocessor (cpp) to take certain actions before compiling. For example, the #include <stdio.h> command in line 1 of hello.c tells the preprocessor to read the contents of the system header file stdio.h and insert it directly into the program text. The result is another C program, typically with the .i suffix.

  2. Compilation Phase Converts the pre-processed code into assembly language. Assembly describes low level machine language that has a common output shared between many languages. This allows C to compile into Fortran and vice versa, for example. Output is usually suffixed with '.s'
    1 main: 
     2 subq $8, %rsp 
     3 movl $.LC0, %edi 
     4 call puts 
     5 movl $0, %eax 
     6 addq $8, %rsp 
     7 ret
    
  3. Assembly Phase The assembler (as) translates '.s' files into machine language instructions and packages them into something called a relocatable object program in '.o' format. It is a binary file containing 17 bytes to encode the instructions.

  4. Linking Phase If a program uses a function specific to C library rather than assembly language, there will need to be a library of relocatable object programs that can perform that function. For example printf is a C function, so the linker (ld) calls in the printf.o executable object file and merges it with the assembly code. Then it is ready to be executed as a whole by the machine.

References