When you type gcc main.c and hit enter

Melissa Ng
3 min readJan 18, 2018

--

Yes computer?

When I write a program to have the computer say hi to me, how did it really know to do that? It’s beautiful to code using pure human readable English, type “gcc <filename>” on a terminal, hit enter, and have a computer actually execute the program. What is the computer really doing?

Amazingly, this simple three letter command “gcc” transforms our human readable code to machine readable code that our computers can understand. Outstanding! “gcc” stands for GNU Compiler Collection and supports several programming languages including C, C++, and Go.

Let’s say I’m going to execute my simple C program with the filename “main.c”. When I type “gcc main.c” the compiler will transform my code through four steps without stopping. We can still slow it down and analyze it.

Step 1: Preprocessor

Before anything happens, lines that start with “#” are processed. It includes code from header and source files. Comments are removed. Macros are expanded with code. We can stop at this stage and view it:

gcc -E main.c

Step 2: Compiler

The file then goes through a compiler that transforms this preprocessed human readable code into assembly code, which is closer to the machine language. There are certain flags that can be added to specify specific assembly codes to match styles such as the Intel syntax. We can stop at this stage and view the assembly code at main.s by typing:

gcc -S main.c

Step 3: Assembler

The assembler translates the assembly code into an object code which is now machine readable code. View this with:

gcc -c main.c

Step 4: Linker

Remember, “ gcc main.c “ goes through all four stages without stopping in between

The object file is then linked with libraries and becomes one executable file. By default, the executable file will be named “a.out” but we can optionally name it ourselves to just “main”:

gcc main.c -o main

After these four transformations, the computer knows to say hi to me. Of course, this simple program told the computer to say hi to me specifically. Imagine what more complex programs can do. That’s the first step in how we talk to machines.

--

--