Compilation Process using in programs like C.
https://es.ccm.net/faq/2821-la-compilacion-y-los-modulos-en-c-y-c#1-el-preprocesado

Compilation Process using in programs like C.

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.

Steps of compilation

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

No hay texto alternativo para esta imagen

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.

  • Preprocessing

Peprocessing is the first step. The preprocessor obeys commands that begin with # (known as directives) by:


- removing comments
- expanding macros
- expanding included files
 

        
No hay texto alternativo para esta imagen

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.

No hay texto alternativo para esta imagen

  • Compiling

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.        
No hay texto alternativo para esta imagen

using the gcc -S command

The compiler will take the preprocessed file and generate Intermediate Representation code, so this will produce a “.s” file.

No hay texto alternativo para esta imagen

  • Assembly

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.        
No hay texto alternativo para esta imagen

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”.

No hay texto alternativo para esta imagen

  • Linking

Linking is the final step of compilation. The linker merges all the object code from multiple modules into a single one.        
No hay texto alternativo para esta imagen


The linker creates the final executable, in binary, and can play two roles:

  • linking all the source files together, that is all the other object codes in the project.
  • linking function calls with their definitions.

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

No hay texto alternativo para esta imagen


要查看或添加评论,请登录

Juan Sebastian Gonzalez的更多文章

  • Recursion

    Recursion

    What is it? Essentially is a technique that split a situation or a problem into shorter pieces of itself. Take a look…

  • Mutable, Immutable... everything is an object! O.o

    Mutable, Immutable... everything is an object! O.o

    In Python, everything is an object, which means every entity has some metadata (called attributes) and associated…

  • Differences between static and dynamic libraries

    Differences between static and dynamic libraries

    A library is a powerful tool in a programmer's arsenal, consisting of pre-compiled functions that can be easily reused…

  • How integers are stored in memory using two’s complement

    How integers are stored in memory using two’s complement

    A computer is an amalgamation of hardware and software, where digital memories store information in bits (short for…

  • The worst abuse of the C preprocessor (IOCCC winner, 1986)

    The worst abuse of the C preprocessor (IOCCC winner, 1986)

    obfuscation is the deliberate act of creating machine code that is difficult for humans to understand. This article is…

  • C static libraries

    C static libraries

    This article is about exploring in C what are the static libraries, why should we use them, how they work, how can be…

  • Soft Link Vs. Hard link

    Soft Link Vs. Hard link

    Understanding the difference between a hard link and a symbolic link requires knowledge of how computers store and…

社区洞察

其他会员也浏览了