Sep 14, 2017 · 2 min read

What happens when you type gcc

C is a high level language that needs a compiler to convert it into executable code so that the program can be run. There are 4 stages of compiling a C program:

  1. Preprocessing:

The C preprocessor modifies a source code file before passing it through the compiler. During this stage, the preprocessor will remove comments, expand macros, and expand included files.

2. Compilation:

In this stage, the preprocessed code is translated to assembly instructions in order to be passed through to the assembler.

3. Assembly:

During this stage, the assembler will now convert the code into binary. The file will now contain machine level instructions for the computer to understand the code.

4. Linking:

To produce an executable program, the existing pieces have to be rearranged and the missing ones filled in. This process is called linking. The linker will arrange the object code so that functions can be successfully called.

Lets create a simple C program called main.c. The extension “.c” tells the compiler to execute a C program.

After the program is written, the code will go through the compiler to be converted into an executable program. Once you save the file and exit emacs, you will now enter into the terminal the gcc command to compile the hello.c file. The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages.

After running the code through the compiler you will now see the a.out file alongside your hello.c file. The a.out file is the executable file that you can now run. The default a.out name stands for assembler output.