What happens when you type gcc main.c

What is GNU?

In 1983 Richard Stallman announced the GNU Linux project (pronounced: GNOO LIN-əks), which was developed as a derivative of the UNIX operating system. The project gained so much popularity that Richard and others founded the Free Software Foundation in 1985. Richard has been very outspoken on some of the early development of GNU and Linux. Research shows that the OS known as Linux is based on the Linux kernel but all other components are GNU based. Many software enthusiast believe that for Linus Torvalds to name Linux.. well.. Linux was unethical and that it should have been named GNU/Linux or GNU Linux to give credit to Richard’s contributions.

What is gcc?

The abbreviation gcc stands for GNU Compiler Collection and was originally written to be the compiler for the GNU OS. It contains compilers for the following languages:

C, C++, Objective-C, Fortran, Java, and more.

The gcc compiler was written in C and accepts options and file names as arguments so that engineers can have absolute control over the compilation process.arguments so that engineers can have absolute control over the compilation process.

$ man gcc

As you can see above there are many different options to pass into the gcc function and at the very end you can see in @file and an infile. This just basically lets a programmer specify what file should be used as input and what file they would like to specify as their output file. Many advanced software projects utilize the output of one file as the input into another file to solve a task.

What is C?

C is one of the top 10 most popular programming languages in the world. According to data from GitHub it is currently the #8 most popular language on all GitHub repositories.

Typically programming languages fall into two categories:

Interpreted languages — for example Python is interpreted into C and from there compiled down in the same fashion as C.

Compiled languages — the C programming language is a compiled language which means it is preprocessed, compiled, assembled, and finally linked as it runs the program (this is the process ran when gcc main.c is ran from your terminal.

C programs start out in human-readable form and in order to run a .c file you must have access to a C compiler. If you are using a UNIX machine (such as a Mac), the C compiler is available for free thanks to that guy Richard and is named gcc. Please note that sometimes it is also referred to as cc.

What happens when you type $ gcc main.c ?

This command runs four steps always in the same order every single time. They are: preprocessing, compilation proper, assembly and linking. The gcc command always runs the file in that order.

Step 1: Preprocessing

Preprocessing is where gcc looks for lines in the file that have hashes and interprets them.

gcc main.c

Step 2: Compilation

Next, the output of the preprocessor is passed into the compiler and the compiler generates assembly code. The assembly code generated is particular to the instruction set of the processor inside your computer. The most common of which are ARM and x86. The ability to utilize different assemblers enables gcc to turn C source code into machine code that can work on a plurality of computer architectures. Assembly code is the least human readable form of code before it becomes machine language, which is virtually impossible to read and interpret.

To generate assembly code from a C source file take a look below

$ gcc -S main.c

Step 3: Assembly

Since our computers cannot interpret assembly code, the job of the assembler is to convert assembly code into binary code (base2) since that is what the computer’s metal can actually read and write to.

The assembler accepts the output of the compiler and turns it into machine code. Machine code is just an executable binary file that contains instructions for the CPU (central processing unit) to interpret. The output of the assembler is put in a file called main.o . This output becomes the final input into what is called the linker in the compilation process.

Before we get into the linker though let’s take a look at what our program looks like after we run the assembler on it.

Step 4: Linking

The last step before we are done with compilation is called linking. In linking, The linker accepts the main.o as input and it also accepts any pre-compiled libraries that were imported with the #include preprocessor directive. Next, it merges the unique (non duplicated parts) to make what is called a standalone executable binary. The linker should only link the functions out of a library that you declared. For example printf would be imported instead linking in every function, used or unused, from the stdio library. Since we did not use a compiler flag indicating otherwise and we did not use the -o flag in gcc , so the executable file will be saved to a.out (which is the default) in the current working directory.

Lets see how this looks after running the following command

$ gcc main.c$ ls
$ main.c a.out

Now we have our executable file named a.out. It’s a default file that is created if we don’t specify what file we should put our executable code in. If we wanted to specify a different output file we would do something like this

$ gcc main.c -o output$ ls
$ main.c ouput

Finally we can go ahead and execute the code using the command

$ ./output

If we did not specify a new file using the -o flag we would execute the a.out file like so

$ ./a.out

Finally you will see the desired output of the “Hello World” program

$ ./a.out
Hello World!
$

--

--

--

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

Recommended from Medium

Creating a New dictionary for my Kindle (Spanish-Traditional Chinese)

MDM Upgrade…

Makers Academy Pre-Course: The Experience in 7 Hors d’oeuvres

New At Programming

How to Record Your First Macro

Swipe down/up to dismiss — Android

Integration of Jenkins ,Docker & GitHub

Introducing DevSpace Cloud: Turn any Kubernetes Cluster into a Powerful Developer Platform

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
joel silva

joel silva

More from Medium

PUMA Clyde Hardwood Review: A Basketball Shoe You Should Know About

DAIRO: Behind the Scenes in Title Sequence Creation

War in Europe, 2022

Types of Self-balanced Binary Search Trees and Their Applications