`Wise raccoon harnesses the power of gcc

A gcc tl;dr

Yared Torres
3 min readFeb 4, 2021

--

What is gcc?

GCC is a C compiler made for GNU operating systems. A compiler is a software program that can analyze programs developed in a programming language and translate it into a language that can be executed by the computer system.

First we must type our program into a file and name that file and end with the c extension .c This is what would be referred to as our source code. In our example we will use the program main.c

To initialize compilation we will use the command gcc main.c

The compilation itself has 4 stages: preprocessor, compiler, assembler, and linker.

Preprocessor

The preprocessor removes comments, includes header files in source code, and replaces macro names with code. The compiler also examines each program statement to check if it conforms to the syntax and semantics of the language. If there is an error, the user is notified of the failure and the process ends.

Compiler

The compiler generates the assembly code of the program. The compiler takes each program statement and translates it into a lower form. What is left is the same program but in assembly language, also known as assembly code.

Assembler

The assembler takes the assembly code and converts it into binary format. This is known as object code. Object code is written to another file in the system, typically written as its original name followed by the file extension .o (for object) .

Linking

This is the final stage and leaves us with a fully executable file. If the program uses other programs that were previously processed by the compiler, they are linked together in this stage. Programs from the system program’s library are searched and linked. The process of compiling and linking is called building.

This final linked file is in executable object code format and is stored on another file ready to be run or executed. By default this file is called a.out on Unix based systems. On Windows they would keep the source file name and replace the .c extension for .exe .

The a.out file can be renamed using the gcc -o option.
$ gcc main.c -o wiseraccoon
$ ls
$ main.c wiseraccoon.out

Using the gcc command

You can use the options -E, -S, and -c to stop the compiling process at different stages.

$ gcc -E main.c
Stops before compiler stage. This would produce a preprocessed source code that is sent to standard output.

$ gcc -S main.c
Stops before assemble stage. This is assembly code, source code that has been compiled but not assembled. This will produce a file with the suffix .s.
$ls
$ main.c main.s

$ gcc -c main.c
Stops before linking stage. This is machine code, source code that has been compiled and assembled, but not linked. This will produce a file with the suffix .o
$ls
$ main.c main.s main.o

May the wise raccoon guide you further.

--

--

Yared Torres
Yared Torres

No responses yet