C/C ++ preprocessing

Source: Internet
Author: User

Let's discuss the preprocessing in C/C ++ today.

Preprocessing

(1) concept: preprocessing refers to the work done before the first scanning (lexical scanning and syntax analysis) of compilation. PreprocessingProgramming LanguageAn important feature, which consists of preprocessingProgramCompleted. When a source file is compiled, the system will automatically reference the Preprocessing Program to process the preprocessing part of the source program. After the processing is completed, the system will automatically compile the source program.

A program that processes a pre-processing statement is called a pre-processing program, also known as a pre-processor, which is included in the compiler. The Preprocessing Program first reads the source file. The pre-processing output is a translation unit, which is stored in the memory to listen to temporary files. The compiler accepts the output of preprocessing andSource codeThe target file containing machine language commands.

The pre-processor is used to replace resources with equivalent values through the built-in pre-processing functions.

(2) Common preprocessing: File Inclusion, Conditional compilation, layout control, and macro replacement.

<1> File Inclusion:

1. # include <File Name> the compiler searches for files from the directory set by the compilation system, so it is often used to include system header files, such as iostream. h, math. h.

2. # include "file name" the compiler searches for files from the current working directory. If the files are not found, the compiler searches for the files that contain the files.

<2> macro replacement: # define, which can be used to define symbol constants, function functions, renaming, String concatenation, and other functions.

<3> Conditional compilation: # If, # ifndef, # ifdef, # endif, # UNDEF, and so on are common preprocessing tasks. They are mainly selected during compilation, comment out some specifiedCodeTo achieve version control and prevent repeated inclusion of files.

For example, it is often used to differentiate # ifdef _ debug compiled by debug and release:

 
IntMain (IntArgc,Char* Argv [])
{
# Ifdef _ debug//For debug Compilation
...
Code for printing detailed logs
...
# Else//For release Compilation
...
Print common Log Code
...
# Endif
Return 0;
}

<4> layout control: # progma provides unconventional control flow information for compiled programs.

1. # progma comment (...): This command puts a comment record into an object file or executable file. Common lib keywords can be used to link a library file, such as the # progma comment (Lib, "user32.lib") command to add the user32.lib library file to the project.

2. # progma once: you only need to add this command at the beginning of the header file to ensure that the header file is compiled once.

3. # pragma Pack: Memory alignment is not described here.

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.