“gcc main.c”, What does it do? 🧐

A H
6 min readJan 18, 2018

--

Where to start? 💭

To better understand what the command “gcc main.c” does, let’s break down each part of the command itself. I think starting with the second part, “main.c” makes more sense for right now, so let’s do that.

Part 2 of the command “gcc main.c”: main.c:

Any file in the programming language C, must end with the extension, “.c”. Why might you ask? This is so that when the C program is compiled, the compiler knows the program itself is a C file. The “main” portion is just what the title of the file is named and is arbitrary, in other words, it could be “hello.c” or “why.c” or “this_is_a_file.c” instead of “main.c” and it would still do the same thing, and still be a C file.

At this point you might be wondering what the word, or rather action “to compile” means, so let’s break that down as well. To understand the action of “compiling”, let’s break down what a compiler is. According to Wikipedia, a compiler is:

computer software that transforms computer code written in one programming language (the source language) into another programming language (the target language). Compilers are a type of translator that support digital devices, primarily computers. The name compiler is primarily used for programs that translate source code from a high-level programming language to a lower level language (e.g., assembly language, object code, or machine code) to create an executable program

What does Wikipedia mean?

🙌 Simply: a compiler is a bit of software created by a group of engineers that takes the code written by the programmer (source code) and translates it to machine code (binary) that the computer understands, generally a compiler then takes options which will relay the output of the code it read in binary to another file (the output file) which results in the output of your program.

🧠 Non-Simply: compiling a program consists of four different parts:

  • Preprocessing
  • Compilation
  • Assembly
  • Linking

To explain each of these steps, we’ll need to take a look at a standard C program, for this example we’ll use the typical “Hello, world” program.

A simple demonstration of the typical “Hello, world” program with comments
  • The preprocessing phase looks for all the “#” characters in your program, usually these are either to include header files, or macros or lastly preprocessor directives that start with “#define”. This stage can get particularly complicated technically, so just note that anything the compiler sees that matches a macro, directive or a header file’s functions gets replaced to make translating the code more efficient for the compiler when it gets to the compilation phase, stage 2. It’s worth noting that if you’d like to print the results of the preprocessing stage you can type (gcc -E hello.c):
A demonstration of how to view the preprocessor phase and some sample output: “gcc -E hello.c”
  • The compilation phase is when source code written by the programmer is translated from the given programming language, in this case C, to processor specific assembly language instructions. This step is what makes C portable in that it can be used on multiple systems with various architectures, e.g., 64-bit and 32-bit etc. You can access the results of this phase by typing (gcc -S hello.c):
An example of output for the compilation phase with a demonstration of the command: “gcc -S hello.c”
  • The assembly phase is where the assembly instructions from the compilation phase is translated to binary or machine code. The output of this phase is the actual 1’s and 0’s the processor uses to produce output based on, “on and off” states. You can access the results of the assembly phase by typing (gcc -c hello.c and then using another command like “od” to output the results):
Displays the results of the assembly phase, you can access this by typing: gcc -c hello.c && od -c hello.c
  • The last phase, the linking phase is when the binary code that was generated in the assembly phase is filled of any gaps or out of order parts that may have been generated prior. This phase is required in order to produce an executable that the end-user-programmer can use to see the results of their program. This mostly consists of making sure functions in specific areas can call functions in other places, in the case of hello.c, the linker would add binary code for the printf command. After this phase has completed, the result is the final executable. You can see this phase by running the command (gcc hello.c -o hello):
Results of normal compilation printed: gcc hello.c -o hello

The “-o hello” portion above specifically outputs the results of compilation to a file, “hello” which can then be executed by typing “./hello” which reveals “Hello, world.” — AWESOME stuff. 😜

Final thoughts on what the “main.c” portion of “gcc main.c” does:

  • main is an arbitrary name that was provided by the programmer and can be almost anything the programmer wants (with some exceptions being special characters, capital letters and numbers, i.e., 1.c and &.c are not valid )
  • All C program files end in “.c”
  • There are 4 steps a compiler goes through to create an executable program: 1: preprocessing, 2: compilation, 3: assembling, 4: linking

Part 1 of “gcc main.c”, gcc:

So what is gcc? I bet by now you may be able to answer that question, but since I’m writing a blog post here, I’ll help out a little: gcc is a C and C++ compiler.

Now that we know that, and we’ve described the steps of compilation above, there’s not much more to mentioned about gcc except for it’s history. With that said, here’s the histroy of gcc:

gcc stands for GNU Compiler Collection which, according to Wikipedia, is a compiler system produced by the GNU Project. gcc is a key component of the GNU toolchain and the standard compiler for most Unix-like Operating Systems, including Ubuntu and others. Another cool thing about gcc is it’s free and open-source, like all of the software GNU produces. gcc was initially release nearly 30 years ago on May 23, 1987 by GNU and authored by Richard Stallman et all., for the C programming language. It was also licensed under GPL which allowed programmers who wanted to fork or copy the compiler to other programming languages to do so which made it popular and widely used. Some key features that set gcc apart from other compilers include:

  • 🔗 Link-time-optimization which optimizes across object file boundaries to directly improve the linked binary
  • 🔌 Plugins that can extend the GCC compiler directly allowing for improvements on the stock compiler
  • 🤯 Transactional memory which is a concurrency control mechanism for controlling access to shared memory in concurrent computing

Final thoughts on gcc:

  • gcc was initially released in 1987
  • gcc is open-source
  • gcc was developed and released by GNU and authored by Richard Stallman and others
  • gcc has three key features, 1. link-time optimization 2. plugins and 3. transactional memory

Compilers are cool, ha? 👍

If I missed anything feel free to tweet me @wn_gc.

--

--