Exploring compilers: What does “gcc” do?

Source: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/images/GCC_CompilationProcess.png

So you created your first program, congratulations! But now you have this program that you spent a good chunk of time on, and you don’t know how to make it work. But why can’t I just “./” my file and execute it like normal? Well, with C programs, you are actually going to be compiling it.

Wait…compiling, what does that even mean?

So let’s say you have your program, which is in a programming language. For this explanation, we will be using the language, “C.” So what you actually did was write your source code. When you run the command “gcc,” which is a bash command to compile a source code, it is actually taking your “.c” file and putting it through multiple processes.

For the purpose of this lesson, we will be using the test program “main.c.”

First, upon execution, your source code will be preprocessed and sent into the compiler. This is the stage where any comments you may have added to your program will be removed. If you wanted to see how your source code is processed only up until the preprocessor stage, you could use “gcc -E main.c” on the command line.

From the compiler, your source code is no longer source code, but rather assembly code. This will change your file (in this example we use “main.c”) into “main.s.” If you wanted to see how your source code is compiled all the way up until the assembly state, you could run “gcc -S main.c > main.s” which will allow you to view the output file “main.s” in the assembly code state.

Okay, so now can I run my program? Not quite yet.

From there, your program that you wrote will now go through an assembler, which will convert your code once again to object code, which is why your compiled code up until this point will end in “.o.”

If you wanted to see how this affected your code before it hits its final stage (though not recommended as you won’t be able to interpret it since it converts your code to machine language), you could simply run “gcc -c main.c > main.o” where the “-c” option will prevent your code from hitting the final “linking” stage which we will now discuss.

Now we are finally to the last stage of compiling, with what is called “linking.” This is where the machine will link your object code to its library (in this example, our library is <stdio.h>) and finally finish “compiling” it.

So how do you know if the compiler worked? Well if you don’t specify the name of where you want your compiled source code to be output to (i.e. with the command “gcc file.c -o compiledfile”), it will by default (in Linux) output to “a.out.”

So what now? Well, you can finally run your program! Congratulations and happy coding!