COMPILATION

COMPILATION

All the softwares, programs, websites and apps are written in a certain programming language. Basically, everything we see on the screen of our computers or smartphones are just a lot of code written in different languages and assembled together in a certain way. Every programming language has a different use, this post will focus on C language.

C source files are by convention named with .c extension and we use the command “gcc” to compile C source files. (GCC stands for GNU Compiler Collection and it is a compiler system produced by the GNU Project.)

Compilation is composed by 4 steps:

  1. Preprocessing:

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

  • removing comments
  • expanding macros
  • expanding included files

If you included a header file such as #include <stdio.h>, it will look for the stdio.h file and copy the header file into the source code file.

The preprocessor also generates macro code and replaces symbolic constants defined using #define with their values.

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

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

4. Linking:

Linking is the final step of compilation. The linker merges all the object code from multiple modules into a single one. If we are using a function from libraries, linker will link our code with that library function code.

In static linking, the linker makes a copy of all used library functions to the executable file. In dynamic linking, the code is not copied, it is done by just placing the name of the library in the binary file.

Now that we know what compilation is about, HANDS ON:

Let´s first install gcc:

  1. Open the Terminal in Linux.
  2. Type "sudo apt update" and press Enter.
  3. Type "sudo apt install build-essential" and press Enter to install GCC and other packages.
  4. Type "gcc --version" to verify the GCC installation.
  5. Use the "CD" command to navigate to the folder with your source code. You should find some file to compile (you can use the "ls" command to list the files found in the directory), or create one with a text editor like vi or emacs.

Now let´s compile:

For our example, let’s take a look at a source code inside a file called “main.c”, where “.c” is a file extension that usually means the file is written in C. This picture is inside the text editor vi:

No hay texto alternativo para esta imagen

In order for our main.c code to be executable the compiling process is necessary.

gcc options:

  • To go through all of the four steps in once we need to enter the command line “gcc main.c” in the terminal.

To run the main program, we type “./main” into the terminal (pay attention to the use of the extension, when trying the program ".c" is not mentioned).

  • To go only through the preprocessor step we type in the terminal:

No hay texto alternativo para esta imagen

  • For the compiling step:

No hay texto alternativo para esta imagen

  • Assembly step:

No hay texto alternativo para esta imagen

  • Linking:

We create an executable program with the name we want, by adding the “-o” option to the gcc command, placed after the name of the file or files we are compiling, and pressing enter (there are other ways for this final step):

No hay texto alternativo para esta imagen

YOUR FILE IS NOW COMPILED :)

For more gcc options you con read the manual:

Hope you find it helpful and easy to understand.

To view or add a comment, sign in

More articles by Paulina Crespi

  • What happens when you type google.com in your browser and press Enter

    Step 1: You type google.com(URL) into the address bar of your browser.

  • Internet of Things (IoT)

    What does IoT stand for? IoT stands for Internet of Things. It makes reference to the network of physical objects wich…

    1 Comment
  • RECURSION

    What is Recursion? Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler…

  • Dynamic Libraries vs Static Libraries

    Why using libraries in general? Libraries provide the user the benefit to use a variety of functiones coded in…

  • What happens when you type `ls -l *.c` in the shell

    INTRODUCTION: A Unix shell is a command-line interpreter or shell that provides a command line user interface for…

  • ALL ABOUT C STATIC LIBRARIES

    What is a Library? A library is a collection of code routines (functions, classes, variables, and so on) that can be…

  • HARD & SYMBOLIC LINKS IN LINUX

    What is a Hard Link? A hard link is merely an additional name for an existing file on Linux or other Unix- like…

Explore content categories