A brief introduction to some of the work _c languages of C + + pre-processor

Source: Internet
Author: User

What a delightful question.

Before being taken to the compiler, the preprocessor will take a look at your source code, do some formatting work, and execute any instructions you have left it to execute in the source code.

Like what?

Well, the preprocessor instructions are referred to as preprocessing directives, and they all start with a #.

Like #include?

That's right.

Each # command encountered by the preprocessor causes changes to the source code in some way. Let's do a simple research on them, and then we'll have a look at how it works behind it.

#include

Header files that contain other libraries, classes, interfaces, and so on. The preprocessor actually simply copies the entire header file into your source code (yes, that's why it's a good thing to include defense).
#define

Who doesn't like macros? The preprocessor replaces all defined entities with the defined code. The definition lasts until the #undef instruction of the definition is found.
#ifdef

The conditional behavior tells the preprocessor to contain code in the conditional block that the declared condition is encountered. You can use them like if-else statements, from where you choose: #ifdef, #ifndef, #if, #else, and #elif, and you always end up using a #endif.

#error #warning

Used to send a message to the user. The preprocessor will stop at the #error and not #warning. In both cases he would send the string he found behind the instruction (parentheses) and send it to the screen as output, so it was a manual way to ensure all OK against your platform.
#line

Use to modify the error line numbers and file names that are displayed when you encounter compilation errors. For example, to join you need to view a source file from the compiled intermediate file (which may be auto-generated).
#pragma

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

#assert #unassert

These are always very popular in the old program (well, as long as I've worked for such a program), but they are now obsolete. It's highly recommended not to use them, which means don't put them in the new code.
Predefined macros

There are a number of scheduled Yi Hong available:

__FILE__ gives the filename of a string
__line__ give the current line number (integer)
__DATE__ the current compiled date string
__time__ the current compile time string
__STDC__ is associated with compilers, but is often defined as 1 to declare compatibility with ISO C standards.
__cplusplus in compiling a C + + program is always going to be defined

In particular, the first two are really useful when debugging. Just take out both of them, do not have to write your own files and line processing classes, you can magically allow you to obtain a wealth of information output.


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

1. Replace all three-letter combinations and I'll talk about him in a future article, because although he's just a historical feature (and you're going to switch it in GCC), it still makes it interesting.

2. Divide the source code into multiple lines.

3. Remove all annotations and replace them with a single space.

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

5. Process escape sequences.

6. Send the file to the compiler

If you want to see what your files will look like after preprocessing (who doesn't?), you can pass the-e option to GCC. This will want to stdout the standard output to send preprocessed source code and terminate the GCC command execution directly without compiling and connecting.


For example

g++-E MyFile.cpp

You can also use this parameter:

-save-temps

After compiling, there will be a temporary file.

Take the following simple procedure and say it:

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

Compile with the following line command

g++ Hello.cpp-save-temps

When the compilation is finished, two files are generated in the folder: Hello.s and Hello.ii

Hello.s inside is the assembly code, and HELLO.II is the source code after preprocessing.

Open Hello.ii with a text editor and you'll find a lot more code. That's because #include instructions add the code to the Stdio header file.

If you pull the scroll bar to the bottom, you'll find that the macro definition one and two for the line in printf have been replaced by the preprocessor with 1 and 2.

It's amazing!

In fact, it is only in the compile time, you copy the source code files, as a temporary file, and then the inside of the preprocessing instructions to replace. After use, the temporary document was deleted. So normally we don't know the existence of this file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.