Compilation Contemplation

A green circuitboard
Photo by Michael Dziedzic on Unsplash

Before using any C program, it is essential to first compile it. While a simple

gcc main.c

will compile your code for you, it’s critical to understand the steps that it takes to make your program executable.

Steps of Compilation

  1. Preprocessor
  2. Compiler
  3. Assembler
  4. Linker

Preprocessor

The preprocessor will format your program for the compiler. To do so, it removes comments, includes header files, and performs macro expansion.

To send a file to the preprocessor but not compile it, we will use this command:

gcc -E [filename.c]

Compiler

The compiler takes the output from the preprocessor and turns it into assembly code.

To compile our file but not assemble it, we will use this command:

gcc -S [filename.c]

If we apply this to our main.c file, we get this output:

vagrant@vagrant-ubuntu-trusty-64:~/holbertonschool-low_level_programming/testdir$ gcc -S main.cvagrant@vagrant-ubuntu-trusty-64:~/holbertonschool-low_level_programming/testdir$ lsmain.c  main.s

Assembler

The assembler takes the assembly code and turns it into machine code.

To send our file through every step except for the linker, we will use this script:

gcc -c [filename.c]

if we apply this to our main.c file, we get this output:

vagrant@vagrant-ubuntu-trusty-64:~/holbertonschool-low_level_programming/testdir$ gcc -c main.cvagrant@vagrant-ubuntu-trusty-64:~/holbertonschool-low_level_programming/testdir$ lsmain.c  main.ovagrant@vagrant-ubuntu-trusty-64:~/holbertonschool-low_level_programming/testdir$

You will notice that this gives you the file main.o. While this format is the same as a fully-compiled program, it will not be executable.

Linker

The linker combines the object code from the assembler with library code in order to produce an executable program.

To fully compile our program, we will use this command:

gcc [filename.c]

As you’ll notice, this command will automatically name the file for you. If you want to specify the new name of the file, use this:

gcc [main.c] -o [main]

You will now have an executable file!

--

--

--

Software engineering student and lover of mountains.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build an RPM using Maven and Spring Boot

53. Maximum Subarray

University Management Module Using Odoo 12

Azure Training Day Fundamentals 1 Notes

#1 — Cracking A Simple Crackme

Tutorial: Standing up an EKS Cluster with Terraform

Nifty Docker tricks for your CI (vol. 2)

Understanding functions in 3 levels

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aleia DeVore

Aleia DeVore

Software engineering student and lover of mountains.

More from Medium

Thirty Days of Invention #001

How to operate LDR using Arduino Uno

How to operate LDR using Arduino Uno

The Browser

13 phishing attack types and how to protect your business against them — Privacy Ninja