C Program Compilation

image 1. taken https://computer.howstuffworks.com/c1.htm

For this blog, we will learn all the compilation steps, but first a little introdution to clarify some programming concepts beforehand, we will cover what the C language is, how to compile it with a tool like gcc, and what happens when we compile it.

language C

It is a formal language that, through a series of instructions, allows a programmer to write a set of orders, consecutive actions, data and algorithms to create programs that control the physical and logical behavior of a machine, in short, A programming language is a artificial language designed to express processes that can be reproduced by machines.
We classify programming languages ​​according to the level of abstraction, according to the way of execution and processing of the orders, according to the programming paradigm they use, among others. In order not to delve further into this topic, let’s talk about the C language.

C is a programming language oriented to the implementation of operating systems, specifically UNIX. C is one of the most efficient languages ​​in terms of resulting code and is the most popular language for creating systems software. It is a very versatile language, since it works both at a low and high level and allows a high control over the machine.

C is also a compiled language, as opposed to interpreted, which means that source files written in C must be compiled to be executable.

image 2. image create by Alejandra Higuera

Compilation

In a more graphic and simple way, let’s look at image 2, an analogy to be programmed is a communication system, in which the elements are the person as the source, the message is the set of instructions and codes that we write to the receiver, which is the machine and the feedback is something like the answer or what the machine reproduces us; To talk about compilation, get the idea that the source sends the message in a different language and to reach the receiver, it must be translated, that is where the compilation enters.
The compiler converts the high-level language into instructions in machine code. the c compiler on Linux, is gcc which means “Gnu Compiler Collection “ it is necessary to carry out these four stages in succession:

Image 3. Example

to see a clear and simple example (image3)see this program, the name is hello.c and that will print “Hello, Betty”.

To compile this program, we will be using the gcc compiler, it is used as follows:
$ gcc hello.c
This creates an executable called a.out. To run the executable, you would type
$ ./a.out
Hello, Betty
Now the details when we write gcc compiler:

Compilation model

When you invoked the gcc compiler, a series of steps were taken to generate: preprocessor, compiler, assembler, and linker. So let’s go over each of them.

image 4. taken https://people.sc.fsu.edu/~jburkardt /classes/isc_2012/c_program_compilation.pdf

The preprocessor

Has the functions of removing all the comments in the source file, it includes the code of the header files, which is a file with an .h extension that contains C function declarations and macro definitions
replace all macros (code snippets that have been given a name) with their values
The output of this step will be stored in a file with the extension “.i”, so here it will be in hello.i.
To stop compiling immediately after this step, we can use the “-E” option with the gcc command in the source file and hit enter.

gcc -E hello.c

Compilation

The main.i file is compiled to produce an intermediate file: hello.s, the content of which is assembly code.

We can stop after this step with the option “-S” in the gcc command and press enter.

gcc -S hello.c

Assembly

An assembler is used to translate the assembly instructions into object code. The output consists of actual instructions to be executed by the target processor.
You can tell the compiler to stop at this stage by using the -c flag.

gcc -c hello.c

Linker

The object code generated in the assembly stage is composed of machine instructions that the processor understands. To produce an executable program, the existing parts must be rearranged and the parts that need to be completed. This process is called linking.

references

https://people.sc.fsu.edu/~jburkardt/classes/isc_2012/c_program_compilation.pdf

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store