The compilation process of a C program using GCC involves several steps, including preprocessing, compilation, assembly, and linking. Here are the steps involved in the compilation process with the corresponding command:
- Preprocessing: The preprocessing step involves expanding the C program with the preprocessor directives. To preprocess a C program, use the following command:
gcc -E input_file_name.c -o output_file_name.i
This command generates the preprocessed file with the extension .i
. Replace input_file_name.c
with the name of your C source file, and output_file_name.i
with the desired name for the preprocessed file.
- Compilation: The compilation step involves translating the preprocessed C program into assembly code. To compile a C program, use the following command:
gcc -S output_file_name.i -o output_file_name.s
This command generates the assembly code with the extension .s. Replace output_file_name.i with the name of the preprocessed file generated in the previous step, and output_file_name.s with the desired name for the assembly code file.
3. Assembly: The assembly step involves translating the assembly code into machine code or object code. To assemble the assembly code, use the following command:
gcc -c output_file_name.s -o output_file_name.o
This command generates the object code with the extension .o
. Replace output_file_name.s
with the name of the assembly code file generated in the previous step, and output_file_name.o
with the desired name for the object code file.
- Linking: The linking step involves combining the object code with any necessary libraries to create an executable file. To link the object code, use the following command:
gcc output_file_name.o -o output_file_name
This command generates the executable file with the same name as the object code file. Replace output_file_name.o
with the name of the object code file generated in the previous step, and output_file_name
with the desired name for the executable file.
That’s it! You have successfully compiled your C program using GCC with the preprocessing, compilation, assembly, and linking steps.
To compile a C program using the Linux command-line interface, you can follow these steps:
Open a terminal window and navigate to the directory where your C program is located using the cd command.
Type the following command to compile your C program:
gcc -o output_file_name input_file_name.c
Replace output_file_name with the desired name for your compiled executable file and input_file_name.c with the name of your C source file.
Press Enter to execute the command. The GCC compiler will compile your C program and generate an executable file with the name specified in the command.
If there are no errors in your C program, you can run the executable file using the following command:
./output_file_name
Replace output_file_name with the name of your compiled executable file.
That’s it! Your C program should now be compiled and running on your Linux system. If there are any errors during the compilation process, you can refer to the error messages displayed in the terminal window to fix the issues in your code.