What happens when you type gcc main.c ??
How the Compilation Process Works for C Programs?
C is a compiled language. Its source code is written using any editor of a programmer’s choice in the form of a text file, then it has to be compiled into machine code.
C source files are by convention named with .c extension and we use the command “gcc” to compile C source files. (GCC stands for GNU Compiler Collection and it is a compiler system produced by the GNU Project.)
Four Steps of Compilation: preprocessing, compiling, assembly, linking.
how?
Preprocessing:
Preprocessing is the first step. The preprocessor obeys commands that begin with # (known as directives) by:
- removing comments
- expanding macros
- expanding included files
If you included a header file such as #include <stdio.h>, it will look for the stdio.h file and copy the header file into the source code file.
The preprocessor also generates macro code and replaces symbolic constants defined using #define with their values.
Compiling:
Compiling is the second step. It takes the output of the preprocessor and generates assembly language, an intermediate human readable language, specific to the target processor.
Assembly:
Assembly is the third step of compilation. The assembler will convert the assembly code into pure binary code or machine code (zeros and ones). This code is also known as object code.
Linking:
Linking is the final step of compilation. The linker merges all the object code from multiple modules into a single one. If we are using a function from libraries, linker will link our code with that library function code.
In static linking, the linker makes a copy of all used library functions to the executable file. In dynamic linking, the code is not copied, it is done by just placing the name of the library in the binary file.
How to compile ? using the ‘GCC’, we will use some examples as we use our file source main.c ..
#include <stdio.h>int main(void)
{
printf("Hello, World!\n");
return (0);
}
At the shell prompt, enter the command “gcc main.c” and hit Enter. If it successfully compiles, the shell prompt will be displayed again. If it does not compile, it will display error message(s).
vagrant@vagrant-ubuntu-trusty-64:~$ gcc main.c
vagrant@vagrant-ubuntu-trusty-64:~$ ls
a.out main.c
After main.c is compiled, type the command “ls” to list your directory contents and you will see an executable file named a.out. To run the program, type “./a.out” at the shell prompt and hit Enter. Yay, we see the correct output “Hello, World!” followed by a newline.
vagrant@vagrant-ubuntu-trusty-64:~$ ./a.out
Hello, World!
vagrant@vagrant-ubuntu-trusty-64:~$
If you don’t want your output file to be named a.out, which is the default output filename, you can specify a different output filename with the -o option.
gcc -o <desired_output_filename> <source filename>
Let’s see an example below where we want the output file to be named main.
vagrant@vagrant-ubuntu-trusty-64:~$ gcc -o main main.c
vagrant@vagrant-ubuntu-trusty-64:~$ ls
main main.c
vagrant@vagrant-ubuntu-trusty-64:~$vagrant@vagrant-ubuntu-trusty-64:~$ gcc -o main main.c
vagrant@vagrant-ubuntu-trusty-64:~$ ls
main main.c
To run the main program, we type “./main” into the terminal.
vagrant@vagrant-ubuntu-trusty-64:~$ ./main
Hello, World!
vagrant@vagrant-ubuntu-trusty-64:~$