Behind gcc

What happens when you type gcc on a namefile.c

Sun Jun 23 2019 - 2 min read

C is a procedural programming language that was developed by Dennis Ritchie and many newer programming languages borrowed many features from it like Java, C++, PHP even the popular JavaScript.

C programs usually have an initial structure, so it begins with a Header and then a main() method, inside it a variable declaration, a body, and a return statement.

#include <stdio.h>
int main(void)
{
  int a;
  printf("%d", a);
  return(0);
}

gcc compiler

It is a compiler usually used via command line, and you can use it by simply typing

gcc filename

If you use this command like the example above the default output would be a.out which you can run it. If you want to change the name for the executable file you can use the -o option

gcc filename -o new_output_file

gcc compiles the program and converts the code into machine language. All the processes start with the preprocessing, then compilation, assembly, and finally linking.

Preprocessing

The preprocessor is a separate program that reads the contents of the header and inserts it directly into the program. This is the first stage of the compilation process.

Compilation

The second stage is the compilation, the compiler converts the preprocessed code to assembly language without creating an object file.

gcc -S filename

Assembly

The assembler translates the file into machine language instructions and generates an object file .o.

gcc -c filename

Linking

The final stage, in this phase, is produced as an executable file. All the compilation process finishes when linking and the compilation process is complete.

Last words

This is how gcc works when you are compiling a C program from the command line, we reviewed the four stages and how they are implemented.

Regresar

Enjoy this note? Feel free to share!

X logo Y Combinator logo LinkedIn logo