When writing a program, we often use the #pragma instruction to set the status of the compiler or indicate some specific actions to complete the compiler. Here, some common parameters of the instruction are introduced, I hope to help everyone!
I. Message parameters.
Message It can output the corresponding information in the compilation information output window, which is very important for the control of source code information. The method of use is:
#pragma message ("Message Text")
When the compiler encounters this instruction, print the message text in the compilation output window. When we define many macros in the program to control the source code version, we may have forgotten that there is no correct settings. At this time we can use this instruction to check when compiling. Suppose we want to judge what you have in the source code _X86 this macro can use the following method #ifdef _x86 #pragma message ("_ x86 macro actid!") #ENDIF When we define _x86 this macro, The application will display "_ x86 macro actid!" In the compilation output window when compiling. We will not be scratched because of some specific macro they define.
II. Another more use of the #pragma parameter is Code_SEG. Formats such as:
#pragma code_seg ([{Push | POP},] [Identifier,]] ["Segment-name" ["Segment-Class"] This instruction is used to specify the feature stored in .Obj file, observe OBJ Files can use the VC's own dumpbin command line program, the function is in the .Obj file, the default storage section is .Text section If the code_seg does not have a parameter, the function is stored in the .Text section Push (optional parameter) will record a record In the stack of the internal compiler, the optional parameter can pop up a record from the top of the stack or the tits of the Name, which can be an identifier or an ID number IDIFIER (optional parameter) When using the PUSH instruction, an identifier of the recorded assignment of the stack is pressed, and when the identifier is deleted, the record in the relevant stack will be popped up, "Segment-Name" means function. The name of the store is stored, for example,: // By default, the function is stored in the .Text section 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 () {}
Three. #Pragma ONCE (more common)
This is a relatively common directive, as long as the first instruction is added to the first order, it will ensure that the header file is compiled once.
IV. #Pragma HDRSTOP indicates that the pre-translated header files are so far, and the back header file is not precompiled.
The BCB can pre-translate the header file to speed up the speed of the link, but if all header files are pre-compiled, it may account for too much disk space, so use this option to exclude some header files. There is a dependency between the units, such as unit A dependent unit B, so unit B is compiled by unit A. You can use #pragma startup to specify compilation priority, if #pragma package (smart_init) is used, BCB will compile according to the size of the priority.
Five. #Pragma Warning Directive
This instruction allows the behavior of warning messages for selective modification compilers
The instruction format is as follows: #pragma Warning (Warning-specifier: warning-number-list [; warning-specifier: warning-number-list ...] # Pragma Warning (push [, n]) #pragma Warning (POP) mainly The warning is said to have the following:
Once: Only (Warning / Error, etc.) Message DEFAULT: Reset the compiler's Warning behavior to the default status 1, 2, 3, 4: Four Warning Level Disable: Prohibited Specified Warning Information Error: WARNING Information Error report
If you don't understand the above explanation, you can refer to the following example and instructions #pragma Warning (Disable: 4507 34; Once: 4385; error: 164) Isometric: #pragma Warning (Disable: 4507 34) // Do not show 4507 and 34 Warning Information #pragma Warning (ONCE: 4385) // 4385 Warning Information Only #pragma Warning (ERROR: 164) // Put the 164 warning message as an error. At the same time, this Pragma Warning also supports the following format: #pragma Warning (push [, n]) #pragma Warning (POP) This n represents a warning level (1 --- 4). #Pragma Warning (Push) Save existing warning status of all warning information. #Pragma warning (push, n) Save existing warning status of all warning information, and set the global warning level to n. #Pragma Warning (POP) pops up the last warning message in the stack, canceling all changes made between the stack and out of the stack. 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, re-save all warning information (including 4705, 4706 and 4707)
Many warning messages are often obtained when programming using standard C , and these warning messages are unnecessary tips, so we can use #pragma Warning (Disable: 4786) to ban the type of warning
When using ADO in the VC, unnecessary warning information will be obtained. At this time we can eliminate the type of warning information by #pragma Warning (Disable: 4146).
6. Pragma Comment (...) The format of the instruction is #pragma comment ("comment-type" [, commentstring])
This instruction puts a comment record into an object file or executable, Comment-Type: one of the five predefined identifiers that can be specified as five predefined identifiers:
Compiler: Place the compiler's version number and name in the target file, this section The annotation record will be ignored by the compiler if you provide the CommentString parameter for the record type, the compiler will generate a warning, for example: #pragma comment (Compiler)
Exestr: Place the CommentString parameter into the target file. This string will be placed in the executable when the link is linked. When the operating system loads executable file, the parameter string does not load into memory. However, the string can be found and printed by a program such as Dumpbin, you can use this identifier to embed the information such as version number to the executable!
LIB: This is a very common keyword used to link a library file to the target file.
Commonly used lib keywords can help us connect into a library file. For example: #pragma comment (lib, "user32.lib") This instruction is used to join the user32.lib library file to this project: put a link option into the destination file, you can use this instruction instead of command The link option is incoming or in the development environment, you can specify the / include option to force to include an object, for example: #pragma comment (Linker, "/ include: __ mysymbol")
You can set the following link option in the program
/ Defaultlib / export / include / merge / section These options are not explained here, please see MSDN!
User: Place the general comment information into the target file containing the text information of the comment, this annotation record will be ignored by the linker, for example: #pragma comment (user, "compiled on" __date__ "__time__)