Introduction to Pragma instructions [transfer]

Source: Internet
Author: User

When writing a program, we often use the # pragma command to set the compiler status or instruct the compiler to complete some specific actions.
The following describes some common parameters of this command. I hope it will be helpful to you!

1. Message parameters.

Message
It can be used in the compilation information output window
Output the corresponding information, which is very important for controlling the source code information. The usage is as follows:

# Pragma message ("message text ")

When the compiler encounters this instruction, it prints the message text in the compilation output window.
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
The command is checked during compilation. Suppose we want to determine whether we have defined the _ x86 macro in the source code. The following method can be used:
# 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 heads because we don't remember some specific macros we defined.
 


2. Another one that is used more often # The Pragma parameter 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
If code_seg does not include a parameter, the function is stored in the. text section.
Push (optional parameter) puts a record into the stack of the internal compiler. Optional parameters can be an identifier or a node name.
Pop (optional parameter) pops up a record from the top of the stack. This record can be an identifier or a node name.
Identifier (optional parameter) 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" (optional) 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 (commonly used)

This is a commonly used command. 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 header files are pre-compiled, it may occupy too much disk space. Therefore, 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 before 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.


V. # pragma warning command

This command 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)

The main warnings are as follows:

Once: only display once (warning/error) Message
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 quite familiar with the above explanation, you can refer to the example and description below in the examples.
 
# 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.
This pragma warning also supports the following formats:
# 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 changes 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 the Code, save all warning information (including 4707, and)

Many warning messages are often generated when standard C ++ is used for programming, and these warning messages are unnecessary prompts,
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 comment record into an object file or executable file,
Comment-type: either of the five predefined identifiers can be specified.
Five predefined identifiers are:

Compiler: puts the version number and name of the compiler into the target file. This comment record will be ignored by the compiler.
If you provide the commentstring parameter for this record type, the compiler will generate a warning
Example: # pragma comment (compiler)

Exestr: Put the commentstring parameter in the target file. When linking, this string will be placed in the executable file,
When the operating system loads an executable file, this parameter 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 version numbers
Execution file!

Lib: This is a very common keyword used to link a library file to the target file.


Common lib keywords 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 this project.


Linker: place a link option in the target file. You can use this command to replace the commands passed in 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: Put the commentstring parameter in the target file to include the text information of the comment. This comment record will be ignored by the linker.
For example:
# Pragma comment (user, "compiled on" _ date _ "at" _ time __)

 

The above-mentioned # pragma command does not contain all parameter descriptions. I just provided some relatively common parameters, and there are some improper
Please kindly advise!

 

From: http://www.cnblogs.com/lidabo/p/3553135.html

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.