What happens when you use gcc to compile a C file?
The GNU compiler collection (GCC) was first released May 23, 1987. That was more then 30 years ago, yet it comes standard on many operating systems to this day, including Ubuntu. It can not only compile c code but C (gcc
), C++ (g++
), Objective-C, Objective-C++, Fortran (gfortran
), Ada (GNAT), Go and more.
Today i wrote my first C program, a simple Hello World. When it came time to test my program i had to use the gcc
command to compile it and link it to a exactable file. This process is outlined in the diagram at the top of this post. There are 4 modules of the GCC complier, Preprocessor, Compiler, Assembler and Linker.
The preprocessor takes a c source-code file and does the following 6 things
1. Replace all trigraphs. I’ll actually talk about this in a future post, because although it’s effectively a historical feature (and you have to switch it in GCC), it’s still quite interesting.
2. Concatenate source code split over multiple lines.
3. Remove each comment and replace with a space.
4. Deal with preprocessor directives (lines that start with #). For #include, it recursively carries out steps 1 -3 on the new file
5. Process any escape sequences.
6. Pass the file (i.e. the translation unit, or compilation unit) to the compiler.
When the compiler receives the files from the preprocessor, its job it convert those file into assembly language. This however is still not readable by the computer. The computer can only read binary. Good thing the next module, the assembler, is capable of converting assembly code to binary.
Once the assembler creates all the object code it is passed to the linker. The linker is able to link the binary together to form a an executable file. And that is how we go from a C file to a program that can run on your computer.