What happens when you type gcc main.c

“he always bawls”

am2701
3 min readFeb 7, 2021

Prerequisites

Before continuing, it is whortwhile to understand how a computer runs.

A computer is a set of switches that may or may not pass electrical current. Using these switches, we can instruct the computer to perform calculations. These are machine instructions. This is the first, very basic, instruction level.

Because to program only with binary language is a “little bit” annoying, engineers invented assembler’s language. Which enable to give some instructions more understandable by humans and which can be translate by some machine instructions to computer.

After some years, assembler’s language become hard to evolve. So engineers made another language base on assembler’s language: C language.

What does the dot C file mean ?

C compilers, because there is other compilers than GCC, recognizes that end in two characters “.” and “c” as C program.

This files, in state, can’t be interpreted.

What does gcc mean ?

GCC stand for “GNU Compiler Collection”.

The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project supporting various programming languages, hardware architectures and operating systems. — wikipedia

So now, you know what is GCC and dot C file, I can explain you why use it.

Like I said previously, computer need some different level of instruction but human are not computer. So to make very complicated program, we have to transform C language to machine instructions that are understandable by computer, hence the compilation.

GCC will transform C program (which is indicated by .c) by assembling program and link it.

Compiling processes

Preprocessing

It is a stage when the compiler takes the source code and removes all of its comments that are indicated by a # or /**/. Source code is written in plain text and they’re saved as under .c file. Different computers and operating systems are going to have different instruction sets. The program is set up to be examined.

gcc -E <filenameIn> -o <filenameOut>

Where filenameIn is the filename of the source file and filenameOut is the name you want to give to generated file.

E option indicates we want to stop the compiling generation process just after preprocessing step.

Compiling

This stage is where the computer translates the source code into an assembly language (human readable).

gcc -c <filenameIn> -o <filenameOut>

Where filenameIn is the filename of the source file and filenameOut is the name you want to give to generated file.

c option indicates we want to stop the compiling generation process just after compiling step.

Assembling

This following stage is assembly that converts assembly code into an object code(machine language) which is then written into another file on the system. It is a way of translating into the specific machine instructions of the computer system. This format is in binary. After the program has been translated into this object code, it is ready to be linked.

gcc -S <filenameIn> -o <filenameOut>

Where filenameIn is the filename of the source file and filenameOut is the name you want to give to generated file.

S option indicates we want to stop the compiling generation process just after assembling step.

Linking

Last stage in the compilation process is the object files are linked together generate an executable file which gcc will create the output as an executable file called “a.out”. Once we get it, it means that the compilation is correct and complete.

gcc <filenameIn> -o <filenameOut>

Where filenameIn is the filename of the source file and filenameOut is the name you want to give to generated file.

--

--

No responses yet