Introduction to some of the work of C and C++ preprocessors

  • 2020-04-02 03:10:57
  • OfStack

What a delightful question

Just before being taken to the compiler, the preprocessor takes a look at your source code, does some formatting, and executes whatever instructions you leave in the source code for it to execute.

Like what the & # 63;

Well, preprocessor instructions are called preprocessor instructions, and they all start with a #.

Like #include ?

Correct.

Every # command encountered by the preprocessor causes the source code to be modified in some way.

# include

Contains header files for other libraries, classes, interfaces, and so on. The preprocessor actually just copies the entire header file into your source code (yes, that's why including defense is a good thing).
# define

Who doesn't love macros! The preprocessor replaces all defined entities with the defined code. The definition continues until the #undef directive of the definition is found.
# ifdef

The conditional behavior tells the preprocessor the code contained in the conditional block where the declared condition is true. You can use them like an if-else statement, selecting from here: #ifdef, #ifndef, #if, #else, and #elif, and you always end with a #endif.

# # error warning

Used to send messages to users. The preprocessor will stop at #error instead of #warning, and in both cases it will send the string it found behind the instruction to the screen as output, so it's a manual way to make sure everything is OK for your platform.
# line

For example, add a source file (which may be automatically generated) from a compiled intermediate file that you need to view.
# pragma

Other special instructions interpreted by the compiler. Your compiler documentation will tell you how the instructions are used, and you should not assume they are universal.

# # unassert assert

These are always very popular with older programs (well, as long as I've worked for one), but they are now obsolete. It is strongly recommended not to use them, which means not to put them in new code
Predefined macro

There are many predefined macros available:

Give the filename of a string of files with s/s
__LINE__   Give the current line number (integer)
S string of the current compile date
S string with the current compile time
S/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s /
S ++ will always be defined when compiling a C++ program

Especially the first two are really useful for debugging. Just take both of them out, and you'll get an amazing amount of output without having to write your own files and line-handling classes.


Your compiler may also support other macros, such as the entire list of macros you get here (for GCC).
So what actually happens when you run the preprocessor.

      Replace all the trigrams, and I'll talk about it in a future article, because even though it's only a historical feature (and you'll have to switch it around in GCC, too), it's still interesting.

      2. Divide parallel source code into multiple lines.

      Remove all comments and replace them with a space.

      4. Process (as we mentioned above) preprocessor instructions. For #include, he recursively performs steps 1-3 on the new file :-)

      5. Handle escape sequences.

      6. Send the file to the compiler

If you want to see what your file will look like after preprocessing (who wouldn't ?) , you can pass the -e option to GCC. This will send the preprocessed source code to stdout standard output and terminate the execution of GCC commands without compilation or connection.


For example,
 


g++ -E myfile.cpp

You can also use this parameter:
 


-save-temps

There will be a temporary file when compiled.

Take this simple program:
 


#include <stdio.h>
 
#define ONE 1
#define TWO 2
 
int main()
{
  printf("%d, %dn", ONE, TWO);
  return 0;
}

Compile with the following command
 


g++ hello.cpp -save-temps

When compiled, two files are generated in the folder: hello.s and hello.ii

Hello. S is the assembly code,   Hello. ii is the pre-processed source code.

Open hello.ii with a text editor and you'll see a lot more code because the #include directive adds code to the stdio header.

If you pull the scroll bar down to the bottom, you'll see printf   The macro definitions ONE and TWO on that line have been replaced by 1 and 2 by the preprocessor.

Amazing!!!!

In fact, it is only at compile time, you copy a copy of the source code file, as a temporary file, and then replace the preprocessing instructions inside.


Related articles: