How GCC works as a compiler for C files
What is "GCC"?
The GNU Compiler Collection, commonly known as "GCC", is a set of compilers and development tools available for Linux, Windows, and a wide assortment of other operating systems. It includes support primarily for C, C++, Objective-C, Objective-C++, Java and more. The Free Software Foundation (FSF) wrote GCC and released it as completely free software.
What is a Compiler?
To understand the use and necessity of a compiler, we must first acknowledge how we communicate with computers. Computers do not understand the language we speak in our everyday life, they can only interpret what we call 'machine language' (zero's and one's). The compiler is what comes to the rescue here, it 'translates' the programming languages (code that we write) to machine language. Or in other words, converts our source code to executable instruction file's for the computer.
Install GCC in your terminal
You must install gcc on to your computer to use it. Type the following command into your terminal command prompt:
vagrant@ubuntu:~$ sudo apt install gcc
You can check if gcc is installed by typing the following into your terminal prompt:
vagrant@ubuntu:~$ gcc --version
You should receive the following message:
gcc (Ubuntu 10.2.0-13ubuntu1) 10.2.
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.0
BOOM you are ready to compile!
The Four Steps of Compilation:
In the first stage GCC takes source code (usually a file ending with '.c' for the C language) and obeys commands that begin with # (known as directives) by:
If you included a header file such as #include <stdio.h> , it will look for the stdio.h header file which has the necessary information to include the input/output related functions in our program.
#include <stdio.h>
#include <unistd.h>
By using GCC's "-E" flag we can directly do the preprocessing operation.
$ gcc -E file.c
2. Compiling
In this stage, GCC takes the Preprocessed file as input, compiles it to produce an intermediate compiled output. The output file for this stage produces Assembly code, an intermediate human readable language.
By using "-S" flag with GCC we can convert the preprocessed C source code into assembly language without creating an object file.
$ gcc -S file.c
3. Assembly
This is the third step of compilation. The assembler will convert the assembly code into pure binary (machine code). This code is also known as object code.
By using the "-c" flag in GCC we can convert the assembly code into machine level code.
$ gcc -c file.c
4. Linking
Linking is the final step of compilation in which all the linking of the function calls with their definitions are done. The linker knows where all these functions are implemented (The assembler has left the address of all the external functions to be called). Until this stage GCC doesn't know about functions like printf(), puts() etc. The linker also does a few additional tasks for us. It combines our program with some standard routines that are needed to make our program run, so the final executable size is larger than the input file.
$ gcc -o Output file.c
The above command runs the file "file.c" and produces the final executable file "Output".
Now let's compile a file!
We will use "main.c" as our source file for this example.
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return (0);
}
In the shell prompt, enter command "gcc main.c" and hit the 'Enter' key. If it compiles successfully, the shell prompt will be displayed again. If it does not compile it will display error message(s).
vagrant@ubuntu-focal:~$ gcc main.c
vagrant@ubuntu-focal:~$ ls
a.out main.c
After main.c is compiled, type command 'ls' to list your directory contents and you will see a file named "a.out". To run the program type "./a.out" in the prompt and hit the 'Enter' key. We should see the correct output "Hello, World!" followed by a newline.
vagrant@ubuntu-focal:~$ ./a.out
Hello, World!
vagrant@ubuntu-focal:~$
Renaming the output file
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 test that, we want to change the output name to just "main"
vagrant@ubuntu-focal:~$ gcc -o main main.c
vagrant@ubuntu-focal:~$ ls
main main.c
Now let's run the output file
vagrant@ubuntu-focal:~$ ./main
Hello, World!
vagrant@ubuntu-focal:~$
CONGRATULATIONS YOU HAVE SUCCESSFULLY COMPILED A FILE!
To summarize, the four steps of file compilation are: preprocessing, compiling, assembly and linking.
You can also read the man page on gcc for more information, thank you for reading!
Software Engineer
3 年Good read, like how your article is worded