Starting with GCC
Before learning to use GCC, the following example can help users quickly understand the working principle of GCC and apply it to actual project development. First, enter Code :
Listing 1: Hello. c
# Include
Int main (void)
{
Printf ("Hello world, Linux programming! \ N ");
Return 0;
}
Run the following command to compile and run the program:
# GCC hello. C-O hello
#./Hello
Hello world, Linux programming!
Slave Program From the perspective of the compiler, you only need to simply execute a GCC command, but from the perspective of the compiler, you need to complete a series of very complicated work. First, GCC needs to call the Preprocessing Program CPP, which is responsible for expanding the macro defined in the source file, and inserting the content contained in the "# include" statement into it; then, GCC will call Source code Finally, GCC calls the link program LD to link the generated target code into an executable program.
To better understand the working process of GCC, you can separate the above compilation process into several steps and observe the running results of each step. The first step is to pre-compile. The-e parameter allows GCC to stop the compilation process after preprocessing:
# Gcc-e hello. C-O hello. I
If you check the content in the hello. cpp file, you will find that the content of stdio. H is indeed inserted into the file, and other macro definitions that should be preprocessed are also processed accordingly. The next step is to compile hello. I as the target code, which can be done by using the-C parameter:
# Gcc-C hello. I-O hello. o
By default. the I file is considered as the C language source code after preprocessing. Therefore, the above command will automatically skip the preprocessing step and start the compilation process. You can also use the-x parameter to let GCC compile from the specified step. The last step is to link the generated target file to an executable file:
# GCC hello. O-O hello
When the modular design is used for software development, the entire program is usually composed of multiple source files, and multiple compilation units are formed accordingly, using GCC can well manage these compilation units. Suppose there is a program consisting of two source files foo1.c and foo2.c. to compile them and generate the executable program Foo, you can use the following command:
# GCC foo1.c foo2.c-O foo
If more than one file is processed at the same time, GCC will continue to follow the preprocessing, compilation, and link processes. If we look into it, the above command is roughly equivalent to executing the following three commands in sequence:
# Gcc-C foo1.c-O foo1.o
# Gcc-C foo2.c-O foo2.o
# GCC foo1.o foo2.o-O foo
It is a waste of time to compile a project that contains many source files using only one GCC command. Suppose there are 100 source files in the project that need to be compiled, and each source file contains 10000 lines of code. If you use only one GCC command as above to complete the compilation, then GCC needs to re-compile each source file and then connect all the files. Obviously, this wastes a lot of time, especially when a user modifies only one of the files, there is no need to re-compile each file, because many generated target files will not change. To solve this problem, the key is to use GCC flexibly and use tools like make.