Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codejunk1e/68b655630d9a3444eb4d8d6d0e24eae0 to your computer and use it in GitHub Desktop.
Save codejunk1e/68b655630d9a3444eb4d8d6d0e24eae0 to your computer and use it in GitHub Desktop.

What happens when you type gcc main.c?

First of all, what is gcc? If we want to know What happens when you type gcc main.c, we need to know the base of all, gcc

enter image description here

GCC

enter image description here

GCC stands for GNU Compiler Collection (produced by the GNU - G nu is N ot U nix), that the name say, is a set of compilers for various languages. It provides all the infrastructure for building a entire software, like a front-end builder.

GCC is the way to convert a high level software that is understood by humans to a low level commands that is understood by a machine. In a gross mode, we can create a binary of our application

Features

  • Free
  • Sharable
  • Code source is open for modification

The question: What happens when you type gcc main.c?

We need to start to see our file so, in this example we are going to work with the next snippet

#include <stdio.h>

/**
 * main - Entry point
 *
 * Return: Always 0 (Success)
 */   
int main(void)
{
	return (0);
}

That snippet don't have a relevant action, because in this post, we aren't going to learn C, instead we are going to understand, the process behind the command gcc main.c

The process

The process is a union of 4 steps:

  1. Preprocessor (.c)
  2. Compilation (.i)
  3. Assembler (.s)
  4. Linker (.o) Are 4 but, in the 4, we just create the binary, not execute it.

enter image description here

1. Preprocessor

In this part, is the first program invoked by compiler and process directives like #include, #define and #if. That directives are not specifics of C. We can use it in any type of files If we want to create just this part, we need to execute the following

gcc -E main.c

That line put the main.c just in the first step. So what happens?

enter image description here

2. Compilation

The words compilations means translate a programming language to a machine language, and we can do that with

gcc -S main.c

enter image description here

3. Assembler

In this part, the assembly code generated before it's going to be converted into a object code

gcc -c main.c; cat main.o

enter image description here

If you want to see, what is inside of the main.o object, you have to type

gcc -c main.c; objdump -d main.o

4. Linking

In this part our object, it's going to be linked with other libraries that it require for be execute. The line of this depends of how many libraries with have to link

ld -o hello.exe hello.o ...libraries...

And that's. This is the hard way to compile a main.c, because we can just type gcc main.c and do all the process with a output file call (by default) a.out

enter image description here

Thanks

If you need help, dont doubt to type me in my social media

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment