# Pragma preprocessing instructions

Source: Internet
Author: User
Tags modulus microsoft c

Among all the pre-processing commands, the # pragma command may be the most complex. It is used to set the compiler status or to instruct the compiler to complete some specific actions. # The Pragma command provides a method for each compiler to provide the unique features of the host or operating system while maintaining full compatibility with C and C ++ languages. According to the definition, the compilation instructions are proprietary to machines or operating systems and are different for each compiler. The format is generally:

 
# PragmaPara

Here, para is a parameter. Below are some common parameters.

(1) Message Parameters

The message parameter is one of my favorite parameters. It can output the corresponding information in the compilation information output window.Source codeInformation control is very important. The usage is as follows:

 
# PragmaMessage ("message text ")

When the compiler encounters this instruction, it prints the message text in the compilation output window.

When weProgramDefines many macros to control the sourceCodeIn versions, we may forget whether these macros are correctly set. In this case, we can use this command to check the macros during compilation. Suppose we want to determine whether we have defined the _ x86 macro in the source code, we can use the following method:

# Ifdef _ x86
# PragmaMessage ("_ x86 macro activated! ")
# Endif

After we define the _ x86 macro, the app will display "_ 86 macro activated in the compilation output window during compilation! ". We won't be scratching our heads because we don't remember some specific macros we defined.

(2) Another commonly used Pragma parameter is in the code_seg format, for example:

 
# PragmaCode_seg (["section-name" [, "section-class"])

It can set the code segment where function code is stored in the program. It is used when we develop the driver.

(3)

 
# PragmaOnce//Relatively common

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 widely used in consideration of compatibility.

(4)

 
# PragmaHdrstop

Indicates that the header file is pre-compiled till now, and the header file 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.

(5)

 
# PragmaResource "*. DFM"

Add the resources in the *. DFM file to the project. *. DFM defines the appearance of a form.

(6)

 
# PragmaWarning (Disable: 4507 34; once: 4385; error: 164)

It is equivalent:

# PragmaWarning (Disable: 4507 34)//No 4507 and 34 warning messages are displayed
# PragmaWarning (once: 4385)//Warning Message No. 4385 is reported only once
# PragmaWarning (error: 164)//The 164 warning is used as an error.

This pragma warning also supports the following formats:

 # Pragma  Warning (push [, N]) // here n represents a warning level (1---4 ). 
# Pragma Warning (POP)

# Pragma Warning (push) // Save the existing warning status of all warning information.
# Pragma Warning (push, n) // Save the existing warning status of all warning information, and set the global warning level to n.
# Pragma Warning (POP) // pops up the last warning message to the stack. All changes made between the inbound and outbound stacks 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 ).

(7) # pragma comment (...)

This command puts a comment record into an object file or executable file. Common lib keywords can help us to connect to a library file. For example:

 
# PragmaComment (Lib, "comctl32.lib ")
# PragmaComment (Lib, "vfw32.lib ")
# PragmaComment (Lib, "wsock32.lib ")

Each compilation program can use the # pragma command to activate or terminate some compilation functions supported by the Compilation Program. For example, loop optimization:

# PragmaLoop_opt (on)//Activate
# PragmaLoop_opt (off)//Termination

Sometimes, some functions in the program will make the compiler send a warning that you are familiar with and want to ignore, such as "parameter XXX is never used in function XXX". You can do this:

 # Pragma  Warn-1, 100  //  Turn off the warning message for warning #100  
Int Insert_record (REC * R)
{   /* Function body */   } # Pragma Warn more than 100 // Turn the warning message for warning #100 back on

The function generates a warning message with a unique signature 100, so that the warning can be temporarily terminated. Each compiler has different implementations for # pragma, and the effectiveness in one compiler is almost ineffective in other compilers. You can view it in the compiler documentation.

Supplement -- # alignment between Pragma pack and memory

Many real computer systems have limits on the locations where basic data is stored in the memory. They require that the first address value of the data be K (usually 4 or 8) this is the memory alignment, and this K is called the alignment modulus of the data type ).

The Microsoft C compiler (cl.exe for 80x86) in win32platform uses the following alignment rules by default:

The alignment modulus of any basic data type T is the size of T, that is, sizeof (t ). For example, for the double type (8 bytes), it is required that the address of this type of data is always a multiple of 8, and the char type data (1 byte) can start from any address.

GCC in Linux follows another set of rules (this is found in the materials and has not been verified. Please correct the error ):

Any 2-byte size (including single-byte ?) The alignment modulus of data types (such as short) is 2, while all other data types (such as long and double) that exceed 2 bytes are 4 as alignment modulus. Ansi c specifies that the size of a structure type is the sum of the size of all its fields and the size of the padding areas between or at the end of the field. The padding area is the space allocated to the struct to make the struct field meet the memory alignment requirements. So what are the alignment requirements of the struct itself? Yes, the ansi c standard specifies that the alignment requirement of the struct type cannot be looser than the strictest one in all its fields and can be stricter.

How to Use alignment options in C/C ++

The compilation options in vc6 include/ZP [1 | 2 | 4 | 8 | 16]./zp1 indicates alignment at the boundary of 1 byte, and zpn indicates alignment at the boundary of n Bytes. The N-byte boundary alignment means that the address of a member must be arranged on an integer multiple address of the member size or an integer multiple address of N, and the minimum values of the two must be obtained. That is:

Min (Sizeof(Member), n)

In fact, the one-byte boundary alignment means that there is no holes between the structure members.

The/zpn option is applied to the entire project and affects all structures involved in compilation. To use this option, you can open the project properties page in vc6, C/C ++ page, select the code generation category, and select in struct member alignment.

To use alignment options for some schema definitions, use the # pragma pack compilation command:

(1) # pragma pack ([N])

This command specifies the Compact alignment between the structure and the federated members. While the structure of a complete conversion unit and the compact alignment of the Union are set by the/ZP option. Compact alignment is configured at the data description layer with pack compilation instructions. This compilation instruction takes effect in the first structure or joint description after it appears. This compilation instruction is invalid for the definition.

When you use # pragma pack (N), Here N is 1, 2, 4, 8 or 16.

Each structure member after the first structure member is stored in a smaller Member type or N-byte limit.

If you use the # pragma pack without parameters, the structure members are compact to the value specified in/ZP. The default/ZP compact value is/zp8.

(2) The Compiler also supports the following enhanced syntaxes:

# PragmaPack ([[{push | Pop},] [identifier,] [N])

If different components use pack compilation instructions to specify different compact alignment, this syntax allows you to combine program components into a separate conversion unit. Each occurrence of pack compilation instructions with push parameters stores the current Compact alignment in an internal compiler stack.

The compilation indicator parameter table is read from left to right. If you use push, the current Compact value is stored; if you give a value of N, the value will become a new compact value. If you specify an identifier, that is, you select a name, the identifier is associated with the new compact value.

Each time a pack compilation indicator with a pop parameter appears, the value at the top of the internal compiler stack is retrieved and the value is a new compact alignment value. Compact if you use the pop parameter and the internal compiler stack is empty

The value is the value given by the command line, and a warning message is generated. If you use Pop and specify a value of N, this value will become a new compact value. If you use p o p and specify an identifier, all values stored in the stack

Will be deleted from the stack until a matching identifier is found, the Compact value related to the identifier is also removed from the stack, and the Compact value that only exists before the identifier enters the stack becomes the new compact value. If no matched

Identifier, which uses the Compact value set by the command line and generates a level-1 warning. The default compact alignment is 8.

The new enhancements of pack compilation instructions allow you to write header files to ensure that the Compact values before and after this header file are the same.

(3) stack memory alignment

Stack alignment in vc6 is not affected by the alignment option of structure members. It always maintains alignment and alignment on the 4-byte boundary.

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.