`gcc main.c` in bash

A short and sweet explanation

What is `gcc` ?

C programs need to be compiled so the computer can understand the code in machine language (binary). gcc is a command used to compile C programs. It completes all the above steps (preprocessing, compilation, assembling, linking) in one go.

  1. Preprocessing: handles preprocessor directives (lines starting with #). Also, it does initial processing of continuation lines like \ and removing comments.
  2. Compilation: performed on each output of preprocessor and translates preprocessed code into assembly instructions.
  3. Assembling: translates the assembly instructions into object code.
  4. Linking: produces final compilation output from object files the compiler procduced. The output can be a dynamic library or an executable. It links all object files by replacing references to undefined symbols with correct addresses.

Here are the steps to compile C programs in bash:

Step 1: Open Terminal

Step 2: Create source code.

Using your text editor, create a C program.

For example, type emacs main.c, then enter your code:

#include <stdio.h>
int main()
{
printf("Hello World\n");
}

Step 3: Compile the program

Type gcc main.c This will place an executable output file called a.out.

Step 4: Execute your program

Type ./a.out. This will output the result: Hello World.


You can check out more technical articles in this publication — made by Holberton School’s Batch 3 students.