What happens when you type gcc main.c
First, gcc is the GNU Compiler Collection which is a compiler system. It is used in many different languages including C, C++, Java, Objective C and many others, C is the most popular programming language worldwide. It’s a language that was originally developed by Dennis M. Ritchie and was originally first implemented on the DEC PDP-11 computer in 1972; main.c is the file name that we want to compile that has the C extension. You must compile it in order to run it on your computer, compiling the file will convert the contents of the file into binary code so the computer can execute it. The C compilation process has four main stages: preprocessing, compilation, assembly and linking. These stages happen individually from one another, and take place in that exact order.
When you type gcc:
Preprocessing is where gcc looks for lines in the file that have hashes and interprets them, starting with a # character are interpreted by the preprocessor as preprocessor commands.This is the first stage of compilation process where preprocessor directives (macros and header files are most common) are expanded. To perform this step gcc executes the command internally.
Is translate a programming language to a machine language, and we can do that passed into the compiler and the compiler generates assembly code. The assembly code generated is particular to the instruction set of the processor inside your computer. The most common of which are ARM and x86. The ability to utilize different assemblers enables gcc to turn C source code into machine code that can work on a plurality of computer architectures. Assembly code is the least human readable form of code before it becomes machine language, which is virtually impossible to read and interpret
The assembly code generated before it’s going to be converted into a object code. The assembler converts the assembly code into binary, (aka. machine code or object code). Binary is comprised entirely of 1’s and 0’s. We are going to run the gcc command with “-c” to compile it without going into the linking phase.
Here our object, it’s linked with other libraries that it require for be execute. The line of this depends of how many libraries with have to link. The linker links numerous object code files into a single executable file. That way, if you are working on a program with other people, you can write separate files, but in the end have one central executable file with all of your code combined. The linker also links code from any libraries we are using. There are two types of linking: dynamic and static. Static linking is done at “compile time” by the linker. Dynamic linking is done when you run a file via the operating system. Since this is the final stage that the compiler takes, we can just use the standard gcc command without any options.
Object code is only a portion of machine code (bianry) and can contain offsets and placeholders that might not be found in completed programs. Once the compilation process is over, you’re left with an executable file that will be named a.out by default.