Sitemap

gcc

2 min readFeb 9, 2017

--

gcc is short for GNU Compiler Collection. It’s called a collection because it’s literally a collection different compilers for C, C++, Objective-C, Fortran, Java, and Ada.

What we don’t see when we just run gcc is that it actually goes through 4 steps.

Step 1:

Preprocessing

Here the preprocessor:

  • Takes all of the /*comments*/ and removes them
  • Takes any files that you #include -ed and concatenates it to the program
  • #define s all of the macros

gcc has an option that only preprocesses a C program without doing the other steps.

The command looks something like this:

gcc -E -o [output_file] [input_file]

Done right, the output_file should look something like this:

Press enter or click to view image in full size
first part of the preprocessed code.

Now this code is ready to be compiled.

Step 2:

Compiling

Get Bobby Yang’s stories in your inbox

Join Medium for free to get updates from this writer.

This step is the preprocessed code gets compiled into Assembly code.

You to view what your c code looks like in Assembly, you can run the following:

gcc -S main.c

This spits out a file called main.s that looks like this:

Press enter or click to view image in full size

Step 3:

Assembling

Because currently computer’s don’t understand anything but 1s and 0s, the Assembly code will need to be made into code the computer will understand

You can see what your c code looks like after you run it through the assembler by using the following command:

gcc -c main.c

The result should be illegible.

Press enter or click to view image in full size

Step 4:

Linking

This is the final step of compiling a c program. This step takes all of the machine code and libraries you used and puts it through a linker that makes a single executable file that you can run.

gcc has made it very simple for us to compile our c code into an executable but because every step is separate and modular, we’re able to use gcc to see what our code looks like in the different steps.

--

--

Bobby Yang
Bobby Yang

Written by Bobby Yang

Software Engineer @ Wonder Workshop