What happens when you type the command ‘gcc main.c’

Juan Pablo Montoya
2 min readSep 18, 2020

--

Like the previous articles, we’re going to break down the command, see what the ‘gcc’ command is, what its functions are, and finally what happens when we write ‘gcc main.c’.

gcc means GNU Compiler Collections, this command is in charge of compiling a C or C++ file, to this command you can pass many arguments, for warnings, to change the name of the compiled file, etc.

This is the command syntax:

gcc [-c|-S|-E] [-std=standard]

Let’s see what the command does inside and all its processes:

Image taken from ntu.edu.sg

So the fist process is the preprocessing:

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.

The second process is the Compilation:

The compiler compiles the pre-processed source code into assembly code for a specific processor

You could use:

gcc -S [FILENAME]

That will stop the process at at assembly code.

Third process, Assemble:

Assembly converts assembly code to machine code

gcc -o [FILENAME] [DESIREDNAME]

Fourth process, Linking:

the linker (ld.exe) links the object code with the library code to produce an executable file

ld -o hello.exe hello.o

(Creates the file ‘hello.exe’ from the assembly file hello.o)

Now that we’ve explained how the basis of the command work, let’s explain what it does when we type gcc main.c

gcc main.c

The gcc command as we have just explained will compile the main.c file, but since we did not put an argument to rename the compiled executable, it will create a file with an automatically generated name, that file is called a.out and it is the file that executes the code in main.c

This would be the list of files after we list the current directory after executing the command:

vagrant@vagrant-ubuntu-trusty-64:~$ ls
main.c a.out

When we compile the main.c file, the executable generated, as its name says, acquires executable permissions, so we can use ./a.out to execute the code we have in main.c

--

--

Juan Pablo Montoya
Juan Pablo Montoya

Written by Juan Pablo Montoya

Software Engineer student at Holberton School

No responses yet