C  COMPILATION PROCESS

C COMPILATION PROCESS

No alt text provided for this image
The word compile means to translate programming code into machine executable code. There are different ways to compile source code and make it executable depending on the compiler used. Among these compilers, one of the most famous is GCC (GNU Compiler Collection) which brings support for various languages   (C, C ++, Ada, Java, etc.). Its fame and good performance are linked to the gratuity with which it is distributed. 

The compilation process can be divided into four steps, i.e., Pre-processing, Compiling, Assembling, and Linking.

        
The preprocessor takes the source code as an input, and it removes all the comments from the source code. The preprocessor takes the preprocessor directive and interprets it. For example, if <stdio.h>, the directive is available in the program, then the preprocessor interprets the directive and replace this directive with the content of the 'stdio.h' file.

The following are the phases through which our program passes before being transformed into an executable form:

1. Preprocessor

The source code is the code which is written in a text editor and the source code file is given an extension ".c". This source code is first passed to the preprocessor, and then the preprocessor expands this code. After expanding the code, the expanded code is passed to the compiler.

2. Compiler

The code which is expanded by the preprocessor is passed to the compiler. The compiler converts this code into assembly code. Or we can say that the C compiler converts the pre-processed code into assembly code.

3. Assembler

The assembly code is converted into object code by using an assembler. The name of the object file generated by the assembler is the same as the source file. The extension of the object file in DOS is '.obj,' and in UNIX, the extension is 'o'. If the name of the source file is 'hello.c', then the name of the object file would be 'hello.obj'.

4. Linker

Mainly, all the programs written in C use library functions. These library functions are pre-compiled, and the object code of these library files is stored with '.lib' (or '.a') extension. The main working of the linker is to combine the object code of library files with the object code of our program. Sometimes the situation arises when our program refers to the functions defined in other files; then linker plays a very important role in this. It links the object code of these files to our program. Therefore, we conclude that the job of the linker is to link the object code of our program with the object code of the library files and other files. The output of the linker is the executable file. The name of the executable file is the same as the source file but differs only in their extensions. In DOS, the extension of the executable file is '.exe', and in UNIX, the executable file can be named as 'a.out'. For example, if we are using printf() function in a program, then the linker adds its associated code in an output file.

A simple example to compile a program with gcc:

gcc -o ouput input.c

Which will result in a binary with the name "ouput". Gcc also brings several optimization "flags" which allow to get better performance from the binaries in a predetermined type of machine.

So far we have compiled the programs using the command:

$ gcc [file].c
$


and we execute them through the command

$ ./a.out
<result>
$

This is because the C compilers in general and gcc in particular create an executable named a.out in the current directory if nothing else is specified. ./a.out is the way to run that file. But it is generally more convenient to name executable files from the source files they come from. To do this with gcc we use the -o [file] modifier, so the two compilations that we have seen so far could look like this:

$ gcc -o helloworld helloworld.c
$ gcc -o example example.c
$

and we would execute them correspondingly:

$ ./helloworld
Hello World
$ ./example
sum result: 7
subtraction result: 3
result of the multiplication: 10
result of the division: 2
$

For more information about flags of the compilation: https://man7.org/linux/man-pages/man1/gcc.1.html

        

To view or add a comment, sign in

More articles by Shannel Bejarano

  • Activation Functions

    The activation function returns an output that will be generated by the neuron given an input or set of inputs. Each of…

    1 Comment
  • How to fix HTTP 500 Internal Server Error

    This Postmortem article is based on one of my Holberton School projects, where we have to fix a server which has a 500…

    2 Comments
  • ¿ What is IoT - Internet of Things ?

    The Internet of Things (IoT) is the collection of interconnected electronic devices and sensors that measure, collect…

    1 Comment
  • What happens when you type a command in the Shell?

    first let's go step by step: What is a Shell? A Shell or also known as a Command Interpreter, terminal, prompt, cmd, or…

    1 Comment
  • C - Static libraries

    WHY USE LIBRARIES One of the tools that compilers supply us with are libraries. A library is a file containing several…

Explore content categories