How does the GNU Compiler Collection work?

Diego Acosta
3 min readOct 4, 2021
GCC Logo
“Wait! Gnus don’t come from eggs.”

In next lines, i’ll try to explain you what’s behind all the process of compilation for a C file, using GCC in UNIX.

Oh, so you’ve just written a C code and you’re movin’ to next step? Easy, just type gcc [yourFileName]and that’s all. But now we’re gonna see how it gets to the executable from your C file.

A classic. Why try harder?

Step 1: Preprocessor

The preprocessor removes all the comments (sorry, but your computer doesn’t care about your comments) from your C file. It also interprets all the directives (those lines starting with “#” such as “#include”). Then, the preprocessor creates a file with the same name as the input, that contains an expanded version of your original code. You can check the file created in this step with the following command:

gcc -E [yourFileName]

Yes, it expanded my code too much

Now with the expanded code and the comments removed, it’s all ready for next step.

Step 2: Compiler

Wait a second! Compiler? The compiling process has a step named “Compiler”? Of course, my dear friend. The compiler takes as input the expanded version of your code generated by the preprocessor to an even lower level: assembly code. It’s not a very friendly code, but the machine will understand it better than you, and that’s what matter. If you want to check this step, just write the next command on your terminal:

gcc -S [yourFileName]

I told you it’s not the most friendly thing

Step 3: Assembler

The assembler takes the assembly code version of your original C code an converts it to object code. If you want to know better about the result of this step, simply write the next command:

gcc -c [yourFileName]

Yikes!

Also you can read this generated file with the command:

objdump -D [yourFileName]

Satisfying

Step 4: Linker

Last but not least. The final step of whole process. The linker links (it makes sense) the object code version of your original C code with object code of library files. And now your executable is ready!

Fascinating, really? Here’s another thing you should know: by default, all executables are named a.out, but you may want to give it a more representative name. That’s not a problem anymore, here i let you the solution to it:

gcc -o [yourExecutableName] [yourFileName]

--

--

Diego Acosta

Software Engineer Alumni @ Holberton School - MVD->UY