`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.
- Preprocessing: handles preprocessor directives (lines starting with
#). Also, it does initial processing of continuation lines like
\and removing comments.
- Compilation: performed on each output of preprocessor and translates preprocessed code into assembly instructions.
- Assembling: translates the assembly instructions into object code.
- 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:
Step 3: Compile the program
gcc main.c This will place an executable output file called
Step 4: Execute your program
./a.out. This will output the result: