# Pragma preprocessing instructions
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.