what does gcc main.c really do?

Jack Gindi
2 min readSep 13, 2018

--

You have just completed your very first block of C code — let’s call it main.c — that you want to run. Let’s say it looks something like this:

Hello world.

Well, your computer cannot run this program on its own. It needs the help of a compiler to turn your program into something a computer actually understands: binary, the language made up entirely of 1s and 0s.

GCC (the GNU Compiler Collection) is one of the most widely used compilers on Linux systems. GNU, which stands for the hilariously recursive “GNU’s Not Unix,” is a “Unix-like operating system” which is free of charge. You will most likely see GNU in combination with a Linux kernel, but other kernels based on GNU are available as well. Bundled with GNU comes GCC, the program we will use to compile our code. GCC can compile a whole host of languages including C, FORTRAN, Java, and others.

Compiling code follows four main steps in order to create the executable file that our computer will run. The first of these steps, “preprocessing,” removes comments, which look something like this:

/* this is a comment */

and replaces defined variables with their values. Preprocessing also adds in the code of any included files, such as the incredible common stdio.h.

Next, the “compiler” rewrites your code in Assembly. Assembly is technically human-readable, but it is not particularly understandable in the way that more modern languages are. The created Assembly code then moves into the “assembler.”

The assembler creates blocks of binary code for the computer to run. Depending on the length of your code, the assembler may output more than one block, which is where the “linker” comes into play.

The linker attaches these blocks of binary together and creates an executable file from them. These executables are the files that computers can run. Depending on the operating system, these can have different extensions — one that you might be familiar with, if you use Windows, is .exe. Now you can run the outputted file, and it should print “Hello world.”!

--

--