Compilation process using C

Juan Camilo Gonzalez
2 min readJun 24, 2021

--

Compilation is a process of converting source code into object code. An important part of this process is the compiler. The compiler checks the source code for errors. If this code is error-free, the compiler generates the object code. In C language, gcc is the compiler.

The compilation process has four steps:

· Preprocessor

· Compiler

· Assembler

· Linker

Preprocessor

During the preprocessor, preprocessor will remove commands, include header file code in the file itself (source code). Then, macro name will be replaced by code. At the end, a new file with these modifications and *.i extension (filename.i.) will be created.

Compiler

The compiler will take the file created by the preprocessor (filename.i) and make an intermediate compiled file (filename.s). The code inside this file is an assembly code.

Assembler

The assembler convert assemble code into object code. The file will be turn from filename.s to filename.o. This file has machine level instructions.

Linker

The linker merges all the object files into a single executable file. The linker connects the functions calls with the definitions. It adds some extra code to the final file to create those connections. Also, sometimes a code uses some functions from libraries, in this case, the linker will link our code with that library function code (.h file). At the end, the file created can executable file.

The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project supporting various programming languages. When you want to use gcc in linux, type gcc [program_name].c –o [executable_name] and press Enter. “[program_name].c” is the source code file, and “[executable_name]” is the finished program. After this short procedure, the program will now compile.

Below you will

find some popular options for the gcc.

Specify the Output Executable Name

· gcc main.c -o main

Produce only the preprocessor output with -E option

· $ gcc -E main.c > main.i

Produce only the assembly code using -S option

· gcc -S main.c > main.s

Produce only the compiled code using the -C option

· gcc -C main.c

Sources:

https://www.youtube.com/watch?v=VDslRumKvRA

--

--