From Source Code to Executable: A Step-by-Step Guide with GCC

Omar Best
2 min readJun 8, 2023

--

Have you ever wondered how your code goes from being human-readable to machine-executable? The magic lies in the process of compilation. In this post, we’ll unravel the mystery and dive into the intricate steps involved in compiling code using the GCC compiler. So, let’s get started!

Step 1: Writing Your Code
Before we can compile anything, we need to write the code. Let’s take a simple example of a C program that prints “Hello, World!” on the screen. Save the following code in a file named `hello.c`:

#include <stdio.h>

int main() {
printf(“Hello, World!\n”);
return 0;
}

Step 2: Compiling
To compile our code, we’ll use the GCC compiler. Open your terminal and navigate to the directory containing `hello.c`. Now, execute the following command:

gcc -c hello.c

The `-c` option tells GCC to compile the code without linking. This step generates an object file (`hello.o`) containing the machine code specific to our program.

Step 3: Linking
In this step, we’ll link the object file with any necessary libraries to create the final executable. Execute the following command:

gcc hello.o -o hello

Here, `hello.o` is the object file we generated earlier, and `-o hello` specifies the output filename as `hello`. After successful linking, we’ll have an executable file named `hello`.

Step 4: Running the Executable
Now that we have the executable, let’s run it! Execute the following command:

./hello

Voila! You should see “Hello, World!” printed on your screen. Congratulations, you’ve successfully compiled and executed your code!

Understanding the Compilation Process:

Now that we’ve gone through the steps, let’s understand what happens during compilation:

1. Preprocessing: Before actual compilation, the preprocessor handles directives like `#include` and performs macro expansions. The preprocessed code is then fed into the compiler.

2. Compilation: The compiler (`gcc` in our case) translates the preprocessed code into assembly code specific to the target architecture. It checks for syntax errors, performs type checking, and generates object code.

3. Assembly: The assembly code is converted into machine code instructions by the assembler. This step varies depending on the target architecture.

4. Linking: During linking, the linker combines object files and resolves references to functions and variables. It also links any required libraries, creating the final executable.

Benefits of Compilation:

The compilation process offers several advantages:

1. Portability: Compiled code can be run on any machine with a compatible architecture.

2. Performance: Compilation optimizes code for the target architecture, resulting in efficient execution.

3. Modularity: Compiling individual files allows for incremental development and easier maintenance of large projects.

4. Error Checking: Compilers catch syntax errors, type mismatches, and other issues, helping to identify and fix bugs early on.

--

--