What happens when you type gcc main.c
First we are going to define what a compiler is.
Although there are other compilers, the most common and easiest way to compile a program in GNU / Linux is the gcc compiler, since it is the one included in all distributions.
GCC is an integrated compiler of the GNU project for C, C ++, Objective C, and Fortran; it is capable of receiving a source program in any of these languages and generating an executable binary program in the language of the machine where it should be executed.
The acronym GCC stands for "GNU Compiler Collection". Originally it stood for "GNU C Compiler"; GCC is still used to designate a C build. G ++ refers to a C ++ build.
Syntax.
gcc [option | archive ] ... g ++ [option | archive ] ...
Options are preceded by a hyphen, as is common in UNIX, but the options themselves can be multiple letters; Multiple options cannot be grouped after the same script. Some options then require a file or directory name, others do not. Finally, several file names can be given to include in the compilation process.
Next we will see some examples:
gcc hello.c
compile the C program hello.c, generate an executable file a.out.
gcc -o hello hello.c
compile the C program hello.c, generate an executable hello file.
g ++ -o hello hello.cpp
compile the program into C ++ hello.c, generate an executable hello file.
gcc -c hello.c
It does not generate the executable, but the object object code, in the hello.o file. If you don't specify a name for the object file, use the filename in C and change the extension to .o.
Options.
- c
performs preprocessing and compilation, obtaining the file in object code; does not perform binding.
- E
performs preprocessing only, sending the result to standard output.
-o file
indicates the name of the output file, whatever the stages are completed.
-Route
specifies the path to the directory where the files marked for inclusion in the source program are located. There is no space between the I and the path, like this: -I / usr / include
-L
specifies the path to the directory where the library files with the object code of the functions referenced in the source program are located. There is no space between the L and the path, like this: -L / usr / lib
-Wall
displays all compiler error and warning messages, even some questionable but ultimately easy to avoid by writing your code carefully.
-g
It includes in the generated executable the necessary information to be able to trace the errors using a debugger, such as GDB (GNU Debugger).
-v
shows the commands executed at each compilation stage and the compiler version. It is a very detailed report.
Compilation stages.
The compilation process comprises four successive stages: preprocessing, compilation, assembly, and linking. Going from a human-written source program to an executable file requires these four steps in a row. The gcc and g ++ commands are capable of doing the whole process in one go.
1. Preprocessed.
At this stage the directives to the preprocessor are interpreted. Among other things, variables initialized with #define are substituted in code for their value in all places where their name appears.
2. Compilation.
The compilation transforms the C code into the assembly language of the processor of our machine.
3. Assembled.
Assembly transforms the assembly language program into object code, a machine language binary file executable by the processor.
In large programs, where many source files are written in C code, it is very common to use gcc or g ++ with the -c option to compile each source file separately, and then link all created object modules. These operations are automated by placing them in a file called makefile, interpretable by the make command, which takes care of making the minimum necessary updates whenever any portion of code is modified in any of the source files.
4. Linked.
The C / C ++ functions included in our code, such as printf () in the example, are already compiled and assembled in existing libraries on the system. It is necessary to incorporate somehow the binary code of these functions to our executable. This is the linking stage, where one or more modules are put together in object code with the existing code in the libraries.
All in one step.
In a program with a single source file, all the above process can be done in a single step:
$ gcc -o circle circle.c
The file circle.o is not created; intermediate object code is created and destroyed without the operator seeing it, but the executable program appears there and works.
It is instructive to use the -v option of gcc to get a detailed report of all the build steps:
$ gcc -v -o circle circle.c
In summary:
To produce a single-file source executable:
$ gcc -o circle circle.c
To create an object module, with the same source name and .o extension:
$ gcc -c circle.c
To link an object modules:
$ gcc -o circle circle.o
To link the object modules green.o, blue.o, red.o, already compiled separately, in the executable file colors:
$ gcc -o colors green.o blue.o red.o
I hope I have helped you understand what a compiler is