From the very beginning, we know the importance of the compiler. Without this, our code is just a bunch of characters. The steps for the compiler to compile the program
There are four main types of files: preprocessing, compilation, compilation, and linking. Finally, an executable target file is obtained.
What will happen to each of the four steps? I don't know why. Taking GCC as an example, the main work is as follows:
Cpp will be called during preprocessing,
In the compilation phase, cc is called to obtain the assembler,
Call as in the assembly phase to obtain the target code,
Call the link program ld to obtain the executable file.
Since the four steps (in essence four executable code) have been packaged by the compiler and taken care of for us, our work is easy,
I am afraid there is a question mark in my mind about how these processes are implemented.
I have not thoroughly understood the specific working mechanism in the next three phases, but I can still talk about preprocessing.
Before exploring the pre-processing mechanism, it is necessary to understand what a pre-processor is.
There are more than one pre-processor, and the C/C ++ pre-processor is the lowest one-lexical pre-processor.
This type of pre-processor is mainly used for text replacement, macro expansion, and deletion of comments.
Specifically, the pre-processor cpp is responsible for expanding the Macros in the source file and inserting the "# include" content into this kind of work.
The code can be directly written without any doubt. In fact, it is not empty. After all, Google and Wikipedia have passed.
The source code prepro. c is as follows:
Use the-E parameter to stop the GCC compilation process after preprocessing. Run:
So what is the result after preprocessing? The textures are as follows:
If there is a picture with the truth, we can see that:
The pre-processor replaces the stdio. h content.#include <stdio.h>This line; macro PI is also replaced with 3.1415926; and comments are also deleted.
Summary:
★The C/C ++ Preprocessor does not do anything. It only performs macro replacement and text replacement.
★C/C ++ pre-processing does not perform any syntax check, not only because it does not have the syntax check function, but also because the pre-processing command
It does not belong to C/C ++ statements (this is also the reason why do not add points when defining macros). syntax check is what the compiler does.
★After preprocessing, we get only the real source code.
★Another thing is that GCC is really powerful. If you use an IDE such as VC, you may not be able to see it. preprocessing is such a fun thing.