The Compilation of the C program in Linux is a basic learning skill if you are making your
career in Embedded Linux. This article will help you with Compilation for how to run c program in Linux using GCC compiler. Basically here we will be using the Linux OS platform. This c programming in Linux for beginners Tutorial is very helpful if you are a student. Even if you have any query related to anything you can also ask it on our best forum site PiEST Forum.How To Compile And Run C Program In Linux
The C program is a Medium Level Programming Language. But the programs that we will be writing, should be in binary 1’s & 0’s format. So that it can run on the digital processor or we can say any computer system. To convert it from medium-level language to this binary or machine-level language, we need to use the compiler designed by Kernighan and Ritchie (K & R) in their classic book on C programming language. Here you will learn from end to end how to compile and run c program in Linux
There are multiple steps how to convert the user-level understanding language to machine-level language. If you are new to this then please go through my C Programming Language Tutorial. So here let us start to write a basic C Program In Linux for our understanding of how to compile it.
#include <stdio.h>
#define PIEMBSYSTECH_AGE_IN_YEARS 5
int main()
{
int age = PIEMBSYSTECH_AGE_IN_YEARS;
printf("Hello PiEmbSysTech, Your Age Is = %d\n", age);
return 0;
}
/* Save it with piembsystech.c */
In order to compile the above C program in Linux, we will start right from the creation a .c file using VI Editor. Then after writing we need to save it with any name. But here I have taken an example with piembsystech.c. The piembsystech.c code will be stored in a file as a sequence of bytes. Each byte has a value with some characters. The first character is ‘#’ that corresponds with value 35. The second character is ‘i’ which corresponds to the integer byte value of 105 and so on. The conclusion is that all the codes will be converted into digital bits and stored in a file system for further use.
So till now, we understood that we need to write a c program in a .c file and save it. Then all the C statements must be translated individually into a sequence of instructions to which a processor or machine can understand easily. These instructions should be packaged in a form called executable object program.
Different Steps For Compilation Of C Program
There are different methods that we can use to do it. But practically to understand each compilation steps we will be using UNIX/Linux system. The translation from source code to object code or executable code is performed by a compiler driver. There are multiple compilers, but here we will be using the open-source GCC compiler.
Before starting this, you should confirm that the GCC compiler is installed on your Linux system. If not please install it or else you can also follow our tutorial for how to install it. It will be better if you can go through our how to execute Linux commands in the c program Tutorial. You should use the below GCC command to compile the above code.
[root@host ~]# gcc piembsystech.c -o piembsystech
Whenever you will run this C Program In Linux, the GCC compiler installed on your computer will read this “piembsystech.c” source file. Then it will translate into an executable file named with piembsystech as per the name you have given. The compilation is performed in four sequential phases by the GCC Compiler. These are collections of four separate programs named: preprocessor, compiler, assembler, and linker.
So let understand by doing this in each step by step for our better understanding.
Preprocessing Of C Program In Linux
The Compilation process starts from preprocessing phase in C Program In Linux. These are called Preprocessor directives (e.g. #include and #define) nothing but the macros and header files are most common. It is also called a CPP nothing but the C-Preprocessor. Basically, you will be writing a program code in a ‘.c‘ file format. But after the preprocessing completion, it will be converted into a ‘.i‘ suffix or file extension.
[root@host ~]# cpp piembsystech.c > piembsystech.i
If you will execute the above command then it will add all the stdio.h header file functions used in our code. here printf() & PIEMBSYSTECH_AGE_IN_YEARS macro value with the variable age is 5.
The main objective of this phase is to remove the comments, expanding macros, and expanding included files.
Compilation Of C Program In Linux
In this phase, the main C compiler (ccl) programs will convert all the C program syntaxes into assembly language commands. So here the piembsystech.i will convert it to a piembsystech.s file. Use the below command to compile or convert it from .c to .s file format
[root@host ~]# gcc -S piembsystech.i
The GCC command-line option -S tells the compiler to convert the preprocessed codes to Assembly Language. After this conversion, you can see the contents of the piembsystech.i file created on your computer. When you will look at this assembly code, you can see that the assembly code contains a call to the external function “printf” also. So it is very important is that to learn properly how to execute the C Program In Linux.
Assembly Of C Program In Linux
The assembler (as) will translate this piembsystech.s into machine language instructions, and generates an object file that will be named with piembsystech.o.
[root@host ~]# as piembsystech.s -o piembsystech.o
The above command will generate a piembsystech.o file as it is specified with the -o option. The resulting file contains the machine instructions for the above piembsystech program, with an undefined reference to printf.
Linking Of C Program In Linux
The main objective of this C Program In Linux phase is to links all the object files to create the final executable file. An executable file requires many external resources like the system functions, C run-time libraries, etc. If you look at our above program, then you can see that it calls the printf function to print the ‘Hello PiEmbSysTech, Your Age Is =’ message on the console. This function is having consists of a separate pre-compiled object file printf.o, which must be merged with our piembsystech.o file.
The linker (ld) program performs this task for you in C Program In Linux. The resulting file piembsystech is generated, which is an executable file. Now this file is ready to be loaded into memory and executed by the system. The below command is somewhat difficult, but if you want can make it easier if you will do it frequently.
[root@host ~]# ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 /usr/lib64/crt1.o /usr/lib64/crti.o /usr/lib64/crtn.o piembsystech.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbegin.o -L /usr/lib/gcc/x86_64-linux-gnu/9/ -lgcc -lgcc_eh -lc -lgcc -lgcc_eh /usr/lib/gcc/x86_64-linux-gnu/9/crtend.o -o piembsystech
Execution Of C Program In Linux
After the completion of the generation of the executable file of your C program, you can execute it. After the successful execution of the above command, you can run or execute the “piembsystech” file to get the results with the below commands:
[root@host ~]# ./piembsystech
Output:
Hello PiEmbSysTech, Your Age Is = 5
These commands are executed on the based x86_64 system having GCC (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0. There might be a possibility that it might not work on your system. So for this, you need to check your system environment and use the proper command for the linker.
What I can suggest is that if you are facing any problem, then please no need to worry. You can use the below GCC command to generate the executable file directly without any worry about running of C Program In Linux
[root@host ~]# gcc piembsystech.c -o piembsystech
I hope you would have understood that whenever any compiler is compiling any c program means how it is going through the different phases to complete the executable file generation. I hope you are now able to understand for how to compile and run c program in Linux.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.
Best article to understand the C Program compilation using GCC Compiler.