“This post explains about using gcc to compile C program on Linux”
Compile C program using gcc:
- What is a compiler:
Compiler is just like translator between programing language and machine language. It converts source written in programing language to executable instructions file for computer. For different programing languages different compilers are available. Compilers differs from operating system to operating system.
- Open text editor:
- Write code:
In terminal type: vi sample.c and Enter key. Then we enter in to vi text editor with our filename given. Now type a or i key to go in to insert mode. Then type our C program in it. After typing C program press Esc key,colon(:),w,q and Enter key respectively to save and exit from VI. Here is figure showing source code.
- Compile using gcc command:
Now we are in terminal again.Here we type ls command to see our saved file in the list. Then type gcc sample.c -o sample and enter key. Now the gcc compiler compiles our C file and gives the output as filename we given that is executable.
- Execute output file:
If there are any mistakes or errors in the program the compiler gives warnings and error messages with line number to find out them easily, after correcting them compile once again. If it compiles successfully it gives executable file. To check that file we use ls command and see if it is. If it is, now type ./sample in terminal to execute it. we see the result on terminal.
Actually in compiling process preprocessor adds the necessary files those are included in C libraries. That we are listed in first of our program like <stdio.h> and some other files also generated by compiler . One of those file is object file.