(This article is reproduced on the Internet. It is for your reference only to facilitate the transfer to your own space)
# Pragma is a pre-processing instruction. It is used to set the status of the compiler or 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 parameter. The message parameter is one of my favorite parameters. It can output relevant information in the compilation information output window, which 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. 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) 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. The # pragmacode_seg without the section-name string can be reset at the beginning of compilation, we use the driver when developing it.
(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, but it is not widely used in consideration of 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 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) # pragma resource/*. DFM/indicates adding the resources in the *. DFM file to the project. *. DFM defines the appearance of a form.
(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 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 ).
(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.
(8) · use # pragma pack (n) to change the byte alignment of the C Compiler
In the C language, the structure is a composite data type, and its components can be both variables of basic data types (such as int, long, float, and so on, it can also be a data unit of a composite data type (such as an array, structure, and union. In the structure, the compiler allocates space for each member of the structure based on its natural alignment condition. Each member is stored in the memory in the declared order. The address of the first member is the same as that of the entire structure.
For example, the following structure shows the allocation of member spaces:
Struct Test
{
Char x1; // The offset address is 0.
Short X2; // The offset address is [2, 3].
Float X3; // The offset address is [4, 7].
Char X4; // The offset address is 8.
};
The first member of the structure X1, whose offset address is 0, occupies 1st bytes. The second member X2 is of the short type, and its starting address must be two byte pairs. Therefore, the compiler fills a Null Byte between X2 and X1. The third member X3 and fourth member X4 of the structure exactly fall on their natural peer address, and no additional bytes are needed before them. In the test structure, the X3 member requires a 4-byte bounded boundary and is the maximum boundary unit required by all the members of the structure. Therefore, the natural boundary condition of the test structure is 4 bytes, the compiler fills in three NULL bytes after Member X4. The entire structure occupies 12 bytes of space. Change the C compiler's
The default byte alignment mode. By default, the C compiler allocates data to every variable or data unit based on its natural conditions.
Space. Generally, you can use the following method to change the default peer condition:
· With the pseudo command # pragma pack (n), the C compiler will align according to n Bytes.
· Use the pseudo command # pragma pack () to cancel the custom byte alignment.
In addition, the following method is provided:
· _ Attribute (aligned (N) to align the structure members to the natural boundary of n Bytes. If the length of a member in the structure is greater than N, the maximum member length is used for alignment.
· _ Attribute _ (packed): cancels the optimization alignment of the structure during compilation and alignment according to the actual number of bytes occupied.
The first method above n = 1, 2, 4, 8, 16... is more common.
Application Instance
In network protocol programming, data packets of different protocols are often processed. One way is to use pointer offset
Method to obtain various information, but this method is not only complicated in programming, but also the program is modified once the Protocol changes.
It is also troublesome. After learning about the compiler's allocation principles for the structure space, we can use this
Define your own protocol structure and obtain various information by accessing the members of the structure. In this way,
It not only simplifies programming, but even if the protocol changes, we only need to modify the definition of the protocol structure,
Other programs do not need to be modified, saving time and effort. The following uses the TCP protocol header as an example to describe how to define the protocol structure.
The protocol structure is defined as follows:
# Pragma pack (1) // align in 1 byte mode
Struct tcpheader
{
Short srcport; // 16-bit source port number
Short dstport; // 16-bit destination port number
Int serialno; // 32-bit serial number
Int ackno; // 32-bit confirmation number
Unsigned char haderlen: 4; // 4-bit Header Length
Unsigned char reserved1: 4; // retain four of the six digits
Unsigned char reserved2: 2; // retain two of the six digits
Unsigned char URG: 1;
Unsigned char ack: 1;
Unsigned char PSH: 1;
Unsigned char rst: 1;
Unsigned char SYN: 1;
Unsigned char Fin: 1;
Short windowsize; // 16-bit window size
Short tcpchksum; // 16-bit TCP check
Short urgentpointer; // 16-bit emergency pointer
};
# Pragma pack () // cancel the 1-byte alignment
# The alignment length specified by the Pragma pack. The actual use rules are as follows:
Structure, union, or data member of the class. The first one is placed in a place with the offset of 0, and the alignment of each data member is later, follow the value specified by # pragma pack and the smaller value in the length of the data member.
That is to say, when the value of # pragma pack is equal to or greater than the length of all data members, the size of this value will not produce any effect.
The overall alignment of the structure is performed based on the smaller value between the largest data member in the structure and the value specified by # pragma pack.
Specify the database to be connected
For example, if we use wsock32.lib during connection, you can add it to your project without any effort. However, I think it is more convenient to use the # pragma indicator to specify the database to be connected:
# Pragma comment (Lib, "wsock32.lib ")
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/arthur9984/archive/2008/04/17/2300605.aspx