Compilation Process using in programs like C.
Juan Sebastian Gonzalez
Software Developer | Full-Stack Developer | Javascript | React | Nextjs | Redux | Node.js | Python | Django | DRF | Nestjs | Fast Api | Docker | MYSQL | MONGO | ARANGODB
In programming, there are two types of languages, which are interpreted and compilated.
The code of compiled language can be executed directly by the computer’s CPU.
Example of compiled language – C, C++, C#, CLEO, COBOL, etc.
And an interpreted language is a programming language whose implementations execute instructions directly, without previously compiling a program into machine-language instructions.
In this case, we are focusing on compiled language using C.
C is a compiled language. Its source code is written using any editor of a programmer’s choice in the form of a text file, then it has to be compiled into machine code following the next steps.
Let's say that we have a source code called main.c
In the Linux gcc examples above when we want to stop the compilation process just after what we want. We can use " -[something]" right after the gcc command and before the file name.
Peprocessing is the first step. The preprocessor obeys commands that begin with # (known as directives) by:
- removing comments
- expanding macros
- expanding included files
using the gcc -E command
To stop the compilation, we can use the option “-E” with the gcc command on the source file, and press enter.
Compiling is the second step. It takes the output of the preprocessor and generates assembly language, an intermediate human-readable language, specific to the target processor.
using the gcc -S command
领英推荐
The compiler will take the preprocessed file and generate Intermediate Representation code, so this will produce a “.s” file.
Assembly is the third step of compilation. The assembler will convert the assembly code into pure binary code or machine code (zeros and ones). This code is also known as object code.
using the gcc -c command
The assembler takes the Intermediate Representation code and transforms it into object code, that is code in machine language (i.e. binary). This will produce a file ending in “.o”.
Linking is the final step of compilation. The linker merges all the object code from multiple modules into a single one.
The linker creates the final executable, in binary, and can play two roles:
using the gcc filename or the gcc filename -o output
If we define the name of the output file, we will get a file with that name that we can execute using "./" just before, in the other hand if we don't give a name we will get a file called "a.out".
In both cases, we get the output of wath we program