Pragma preprocessing instructions

Source: Internet
Author: User
Tags compact 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: # pragma para
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 relevant information in the compilation information output window,
This is very important for controlling 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 command to check it during compilation. Suppose we want to determine whether we have defined the _ x86 macro in the source code,
You can use the following method:
# Ifdef _ x86
# Pragma message ("_ 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) The other Pragma parameter that is used more frequently 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 is used when we develop the driver.
 

(3) # pragma once (commonly used)

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,
However, considering the compatibility, It is not used much.


(4) # pragma hdrstop

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,
However, if all header files are pre-compiled, it may occupy too much disk space. Therefore, use this option 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 you use # pragma package (smart_init ),
BCB is compiled based on the priority.


(5) # pragma resource "*. DFM"

Add the resources in the *. DFM file to the project. * DFM includes the form
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.

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 the global warning level to n.
# Pragma warning (POP) pops up the last warning message to the stack, and 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:
# Pragma comment (Lib, "comctl32.lib ")
# Pragma comment (Lib, "vfw32.lib ")
# Pragma comment (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:
# Pragma loop_opt (on) // activate
# Pragma loop_opt (off) // terminate

Sometimes, some functions in the program will give the compiler A warning that you are familiar with and want to ignore,
For example, "parameter XXX is never used in function XXX", you can do this:
# Pragma warn-100 // turn off the warning message for warning #100
Int insert_record (REC * r)
{/* Function body */}
# Pragma warn + 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), which is called memory alignment, and this K is called 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, while 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 that exceed 2 bytes
(Such as long and double) All take 4 as the 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:
# Pragma pack ([{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.
If you use the pop parameter and the internal compiler stack is empty, the Compact 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, and the compact value related to the identifier will also be removed from the stack,
And the Compact value that only exists before the identifier enters the stack becomes the new compact value. If no matching identifier is found,
The Compact value set by the command line will be used, and a level-1 warning will be generated. The default compact alignment is 8.

The new enhancements of pack compilation instructions allow you to write header files to ensure that
Compact values 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.

Http://www.52rd.com/blog/Detail_RD.Blog_sunhuibo_5025.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.