#pragma preprocessing instructions in C + + __c++

Source: Internet
Author: User
Tags compact pack microsoft c
In all preprocessing instructions, the #pragma instruction may be the most complex, and its role is to set the state of the compiler or instruct the compiler to perform certain actions.
#pragma指令对每个编译器给出了一个方法, to give the host or operating system proprietary features, while maintaining complete compatibility with the C and C + + languages.
By definition, the compilation instructions are proprietary to the machine or operating system and are different for each compiler.
The format is generally: #pragma para
Where para is the parameter, here are some common parameters.

(1) Message parameter

The message parameter is my favorite parameter, and it can output the corresponding information in the Compile Information Output window.
This is very important for the control of source code information. It uses the following methods:
#pragma messages (message text)
When the compiler encounters this instruction, it prints the message text in the Compile Output window.
When we define a lot of macros in the program to control the source code version, we are likely to forget that we have the right to set these macros,
At this point we can use this instruction to check at compile time. Let's say we want to determine if we have defined the _x86 in the source code,
You can use the following methods:
#ifdef _x86
#pragma message ("_x86 macro activated!")
#endif
After we have defined the _X86 macro, the application displays "_86 macro activated!" in the compiled Output window at compile time.
We don't scratching because we don't remember some of the specific macros we've defined.

(2) Another pragma parameter that is used more is code_seg

Format such as:
#pragma code_seg (["Section-name" [, "Section-class"]])
It is able to set code snippets that are stored in the function code in the program, and it is used when we develop the driver.

(3) #pragma once (more commonly used)

Adding this instruction at the beginning of the header file ensures that the header file is compiled once, and this instruction is actually available in the VC6.
But there is not much use of it, given the compatibility.


(4) #pragma hdrstop

Indicates that the precompiled header file ends here, and the following header file is not precompiled. BCB can precompile the header file to speed up the link,
However, if all header files are precompiled and may take up too much disk space, use this option to exclude some header files.
Sometimes there are dependencies between units, such as unit a dependent B, so cell B is compiled before cell a.
You can specify the compilation priority with #pragma startup, if you use #pragma package (smart_init),
BCB will be compiled according to the priority size.


(5) #pragma resource "*.DFM"

Indicates that the resources in the *.DFM file are added to the project. Include forms in *.DFM
The definition of the appearance.

        
(6) #pragma   warning (disable:4507 once:4385; Error: 164)
 
    equivalent to: 
    #pragma   Warning (disable:4507)     //  does not display warning messages 4507 and 34th  
    #pragma   Warning (once:4385)           //  No. 4385 warning message only once  
    #pragma   Warning (error:164)          //  the number 164th warning message as a mistake.  

    at the same time this pragma  warning  also supports the following format: 
    #pragma   Warning ( push [, N])  
    #pragma   warning (pop)  
    here n represents a warning level (1---4).  
    #pragma   warning (push) saves an existing warning state for all warning messages.  
    #pragma   warning (push, N) saves the existing warning state of all warnings and sets the global warning level to N.    
    #pragma   warning (pop) pops the last warning message to the stack and cancels all changes between the stack and the stack. For example: 
    #pragma   warning (push)  
    #pragma   Warning ( disable:4705)  
    #pragma   Warning (disable:4706)  
    #pragma   Warning (disable:4707)  
   //....... 
    #pragma   Warning (  pop )    
    at the end of this code, all the warning messages (including 4705,4706 and 4707) are saved again.  


(7) #pragma comment (...)

This instruction places a note record into an object file or executable file.
The commonly used LIB keyword can help us to connect to a library file. Such as:
#pragma comment (lib, "Comctl32.lib")
#pragma comment (lib, "Vfw32.lib")
#pragma comment (lib, "Wsock32.lib")



Each compiler can use the #pragma directive to activate or terminate some of the compilation features supported by the compiler.

For example, for loop optimization functions:
#pragma loop_opt (ON)//activation
#pragma loop_opt (off)//termination

Sometimes, some functions in a program cause the compiler to give a warning that you know and want to ignore.
such as "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
function produces a warning message with a unique signature of 100, so that the warning can be temporarily terminated.

Each compiler differs from the implementation of #pragma in that it is almost invalid in a compiler in another compiler. Can be viewed from the compiler's documentation.


Add--#pragma pack and memory alignment problem


Many actual computer systems have a limit on the location of the underlying type data in memory, which requires the value of the first address of the data to be a number k
(usually it is a multiple of 4 or 8), which is called the alignment of the memory, and this k is called the Zimo number of the data type (alignment modulus).

The Microsoft C compiler (Cl.exe for 80x86) under the Win32 platform uses the following alignment rules by default:
The Zimo number of any basic data type T is the size of T, that is, sizeof (t). For example, for the double type (8 bytes),
Requires that the address of the type data be always a multiple of 8, while the Char type data (1 bytes) can start at any one address.

Under Linux, GCC pursues another set of rules (found in the data, not validated, such as errors, please correct me):
Any 2 byte size (including Single-byte?) The number of pairs of data types (such as short) is 2, and all other data types that are more than 2 bytes Zimo
(long,double, for example) are Zimo numbers with 4.

ANSI c stipulates that the size of a struct type is the sum of the size of all its fields and the size of the fill area between fields or the tail of a field.
The padding area is the space that is allocated to the structure body in order to satisfy the memory alignment requirements of the structure body field. So what is the alignment requirement for the structure itself?
Yes, the ANSI C standard stipulates that the alignment requirements of a struct type cannot be more stringent than the one that is most stringent in all of its fields.


How to use the alignment options in C + +

The compilation options in VC6 are/zp[1|2|4|8| ,/ZP1 is aligned with a 1-byte boundary and, accordingly,/ZPN represents an n-byte boundary alignment.
N-byte boundary alignment means that the address of a member must be arranged on the integer multiple address of the member's size or on an integer multiple address of n, taking the smallest value in them.
Is
Min (sizeof (member), N)

In fact, a 1-byte boundary alignment also indicates that there is no hole between the members of the structure.
The/ZPN option is applied to the entire project, affecting all of the structures participating in the compilation.
To use this option, you can open the Engineering Properties page in VC6, the C + + page, and select the Code generation category, which can be selected in struct member alignment.

To use alignment options specifically for certain structure definitions, you can compile directives using the #pragma pack:


(1) #pragma pack ([n])

This directive specifies the compact alignment of the structure and union members. The structure of a complete conversion unit and the compact alignment of the Union are set by the/zp option.
Compact alignment is set with pack compilation instructions at the data description layer. The compilation directs the first structure or joint description to take effect after its occurrence.
The compilation indicates that the definition is invalid.
When you use the #pragma pack (n), here n is 1, 2, 4, 8, or 16.
Each member of the struct after the first struct member is stored in a smaller member type or n-byte boundary.
If you use a #pragma pack with no parameters, the structure member is compact to the value specified by/zp. The default/zp compact value is/ZP8.


(2) The compiler also supports the following enhanced syntax:
#pragma pack ([[{push | pop},] [identifier,]] [n])

If different components use pack compilation instructions to specify different compact alignments, this syntax allows you to combine the program components into a single conversion unit.
Each occurrence of the pack compilation indicated with the push parameter stores the current compact alignment in an internal compiler stack.
The parameter table that the compilation indicates reads from left to right. If you use push, the current compact value is stored;
If you give a value of N, the value becomes the new compact value. If you specify an identifier, that is, you select a name,
The identifier will be associated with this new compact value.

Every occurrence of a pack compilation with a POP parameter retrieves the value of the top of the internal compiler stack and makes the value a new compact alignment value.
If you use pop parameters 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, the value becomes the new compact value. If you use P O p and specify an identifier,
All values stored on the stack are removed from the stack until a matching identifier is found, and the compact value associated with the identifier is also removed from the stack.
And this compact value that exists only before the identifier enters the stack becomes the new compact value. If a matching identifier is not found,
The compact value set by the command line will be used, and a level one warning will be generated. The default compact alignment is 8.

Pack compilation indicates that the new enhancements allow you to write a header file to ensure that before and after the header file is encountered
The compact value is the same.


(3) Stack memory alignment

The alignment of the stack in VC6 is not affected by the alignment options of the struct members. It is always aligned and aligned on a 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.