Pragma preprocessing instructions
Pragma instructions
When coding, we often use the # pragma command to set the compiler status or instruct the compiler to complete some specific actions.
The following describes some frequently used counters of this command. I hope it will be helpful to you!
1. Number of message partitions.
Message
It can be used in the compilation information output window
Output the corresponding information, which is very important for the control of source code information. Its usage is:
# Pragma message ("message text ")
When the compiler encounters this instruction, it prints the message text in the compilation output form.
When we define many Macros in the program to control the source code version, we may forget whether these macros are correctly set. At this time, we can use this
The command is checked during compilation. If we want to infer whether we have defined the _ x86 macro in the source code, we can use the following methods:
# Ifdef _ x86
# Pragma message ("_ x86 macro activated !")
# Endif
After we define the _ x86 macro, the application will display "_
X86 macro activated !". We won't be scratching our ears because we don't remember some specific macros.
2. Another one that is used more than the limit # The number of Pragma segments is code_seg. Format:
# Pragma code_seg ([[{push | Pop},] [identifier,] ["segment-name" [, "segment-class"])
This command is used to specify the section where the function is stored in the. OBJ file. observe that the OBJ file can use the dumpbin command line program provided by VC. The default Storage section of the function in the. OBJ file
For the. Text Section
Assume that code_seg does not contain the number of segments, then the function is stored in. Text.
Push (optional number of records) puts a record into the stack of the internal compiler. The optional number of records can be an identifier or a node name.
Pop (optional number of records) pops up a record from the top of the stack. This record can be an identifier or node name.
Identifier (optional) an identifier assigned to the record that is pushed into the stack when the push command is used. When this identifier is deleted, the record in the stack associated with it will be popped up.
"Segment-name" (number of optional segments) indicates the node name stored by the function.
For example:
// By default, functions are stored in. Text.
Void func1 () {// stored in. Text
}
// Store the function in. my_data1
# Pragma code_seg (". my_data1 ")
Void func2 () {// stored in my_data1
}
// R1 is the identifier. Put the function in. my_data2.
# Pragma code_seg (push, R1, ". my_data2 ")
Void func3 () {// stored in my_data2
}
Int main (){
}
Iii. # pragma once (frequently used than publish)
This is a command that is frequently used than the connector. You only need to add this command at the beginning of the header file to ensure that the header file is compiled once.
4. # pragma hdrstop indicates that the pre-compiled header file ends here, and the header file after it is not pre-compiled.
BCB can pre-compile the header file to speed up the link, but if all the header files are pre-compiled, it may occupy too much disk space, so this option is used to exclude some header files.
Sometimes there is a dependency between units. For example, unit a depends on unit B. Therefore, Unit B must be compiled prior to unit. You can use # pragma startup to specify the compilation priority,
If the # pragma package (smart_init) is used, BCB is compiled based on the priority.
V. # pragma warning command
This directive allows you to selectively modify the warning messages of the compiler.
The command format is as follows:
# Pragma warning (warning-specifier: Warning-number-list [; warning-specifier: Warning-number-list...]
# Pragma warning (push [, N])
# Pragma warning (POP)
Major warnings are as follows:
Once: only one message (warning/error) is displayed.
Default: resets the warning behavior of the compiler to the default state.
1, 2, 3, 4: four warning levels
Disable: Disable specified warning information
Error: uses the specified warning information as an error report.
If you are not very familiar with the above explanation, you can take a look at the following examples and descriptions.
# Pragma warning (Disable: 4507 34; once: 4385; error: 164)
It is equivalent:
# Pragma warning (Disable: 4507 34) // do not display the 4507 and 34 Warnings
# Pragma warning (once: 4385) // only one warning message is reported once
# Pragma warning (error: 164) // the error message 164 is used as an error.
At the same time, This pragma warning also supports the following format:
# Pragma warning (push [, N])
# Pragma warning (POP)
Here N represents a warning level (1---4 ).
# Pragma warning (push) saves the existing warning status of all warning information.
# Pragma warning (push, n) saves the existing warning status of all warning information, and sets global warning
The level is set to n.
# Pragma warning (POP) pops up the last warning message to the stack.
All modifications are canceled. For example:
# Pragma warning (push)
# Pragma warning (Disable: 4705)
# Pragma warning (Disable: 4706)
# Pragma warning (Disable: 4707)
# Pragma warning (POP)
At the end of this Code, all warning information (including 4707, and) is saved again)
When using standard C ++ for programming, many warning messages are often obtained, and these warning messages are unnecessary,
So we can use # pragma warning (Disable: 4786) to disable this type of warning.
When using ADO in VC, we will also get unnecessary warning information.
# Pragma warning (Disable: 4146) to eliminate this type of warning information
6. Pragma comment (...)
The Instruction format is
# Pragma comment ("comment-type" [, commentstring])
This command puts a staring record into an object file or a runable file,
Comment-type: one of the five pre-defined identifiers that can be specified
Five pre-defined identifiers are:
Compiler: puts the version and name of the compiler into the target file. This gaze record will be ignored by the compiler.
Assuming that you provide the commentstring delimiter number for this record type, the compiler will generate a warning
For example: # pragma comment (compiler)
Exestr: puts the number of commentstring tokens in the target file. During the link, this string will be placed in the runable file,
When the operating system loads a runnable file, the number of bytes string is not loaded into the memory. However, this string can be
Programs such as dumpbin can be searched and printed out. You can use this identifier to embed information such as the version code into
Running file!
Lib: This is a frequently used keyword used to link a library file to the target file.
The frequently used libkeyword can help us to connect to a library file.
For example:
# Pragma comment (Lib, "user32.lib ")
This command is used to add the user32.lib library file to the project.
Linker: place a link option in the target file. You can use this command to replace the commands passed by the command line or in the development environment.
You can specify the/include option to forcibly include an object. For example:
# Pragma comment (linker, "/include :__ mysymbol ")
You can set the following link options in the program
/Defaultlib
/Export
/Include
/Merge
/Section
These options are not described here. For details, refer to msdn!
User: puts the General gaze information into the target file. The commentstring comment number includes the staring text information, which will be ignored by the linker.
For example:
# Pragma comment (user, "compiled on" _ date _ "at" _ time __)
Add one
# Pragma pack (N)
Control alignment as shown in Figure
# Pragma pack (push)
# Pragma pack (1)
Struct S_1 {
Char szname [1];
Int;
};
# Pragma pack (POP)
Struct S_2 {
Char szname [1];
Int;
};
Then
Printf ("S_1 size: % d/N", sizeof (struct S_1 ));
Printf ("S_2 size: % d/N", sizeof (struct S_2 ));
Get 5, 8.
# Pragma usage
# Pragma is a pre-processing instruction in C language. It is used to set the compiler status or to indicate that the compiler has completed some specific actions. According to the definition, the compilation instructions are proprietary to machines or operating systems and are different for every compiler.
The format is generally: # pragma para
In this example, para is the number of partitions. Here are some frequently used partitions.
(1) Number of message partitions. The message counts are my favorite counts, which can be used in the compilation information output window.
Output the corresponding information, which is very important for the control of source code information. Its usage is:
# Pragma message ("message text ")
When the compiler encounters this instruction, it prints the message text in the compilation output form.
When we define many Macros in the program to control the source code version, we may forget whether these macros are correctly set, in this case, we can use this command to check it during compilation. If we want to infer whether we have defined the _ x86 macro in the source code, we can use the following methods:
# Ifdef _ x86
# Pragma message ("_ x86 macro activated !")
# Endif
After we define the _ x86 macro, the application will display "_
X86 macro activated !". We won't be scratching our ears because we don't remember some specific macros.
.
(2) The number of Pragma segments used more than the limit value is code_seg. Format:
# Pragma code_seg (["section-name" [, "section-class"])
It can set the code segment where function code is stored in the program. It will be used when we develop the driver.
(3) # pragma once (frequently used than timeline)
You only need to add this command at the beginning of the header file to ensure that the header file is compiled once. This command is actually available in vc6, but it is not used much considering its compatibility.
(4) # pragma hdrstop indicates that the pre-compiled header file ends here, and the subsequent header files are not pre-compiled. BCB can pre-compile the header file to speed up the link, but if all the header files are pre-compiled, it may occupy too much disk space, so this option is used to exclude some header files.
Sometimes there is a dependency between units. For example, unit a depends on unit B. Therefore, Unit B must be compiled prior to unit. You can use # pragma startup to specify the compilation priority. If # pragma package (smart_init) is used, BCB will be compiled based on the priority.
(5) # pragma resource "*. DFM" indicates adding resources in the *. DFM file to the project. *. DFM contains window
The definition of the appearance.
(6) # pragma warning (Disable: 4507 34; once: 4385; error: 164)
It is equivalent:
# Pragma warning (Disable: 4507 34) // do not display the 4507 and 34 Warnings
# Pragma warning (once: 4385) // only one warning message is reported once
# Pragma warning (error: 164) // the error message 164 is used as an error.
At the same time, This pragma warning also supports the following format:
# Pragma warning (push [, N])
# Pragma warning (POP)
Here N represents a warning level (1---4 ).
# Pragma warning (push) saves the existing warning status of all warning information.
# Pragma warning (push, n) saves the existing warning status of all warning information, and sets global warning
The level is set to n.
# Pragma warning (POP) pops up the last warning message to the stack.
All modifications are canceled. For example:
# Pragma warning (push)
# Pragma warning (Disable: 4705)
# Pragma warning (Disable: 4706)
# Pragma warning (Disable: 4707)
//.......
# Pragma warning (POP)
At the end of this Code, all warning information (including 4707, and) is saved ).
(7) Pragma comment (...)
This command puts a staring record into an object file or a runable file.
The frequently used libkeyword can help us to connect to a library file.
(8) progma pack (N)
Specify the structure alignment! # Pragma pack (n) to set the variable to n-byte alignment. N-byte alignment means the offset of the Start address of the variable. First, if n is greater than or equal to the number of bytes occupied by the variable, the offset must meet the default alignment mode, 2. Assume that N is less than the number of bytes occupied by the variable type, the offset is a multiple of N, and the default alignment is not required. The total size of the structure also has a constraint, which is divided into two situations: Assume that N is greater than the number of bytes occupied by all member variable types, the total size of the structure must be a multiple of the space occupied by the largest variable;
Otherwise, it must be a multiple of N. The following example shows how to use it.
# Pragma pack (push) // save alignment status
# Pragma pack (4) // set to 4-byte alignment
Struct Test
{
Char M1;
Double M4;
Int m3;
};
# Pragma pack (POP) // restore alignment
To test this function, you can use the length of the sizeof () struct!