# Pragma usage of the VC Compiler

Source: Internet
Author: User

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) MessageParameters.
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)Another usage ratioRelativelyMultiplePragmaThe parameter isCode_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 (RatioRelativelyFrequently 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 hdrstopIndicatesPrecompiled headerFile to thisIsStop, followedHeaderFile notIntoLinePre-compile.
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 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 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) UsePragmaExportOutputDLLFunctions in
The traditional way to exit the DLL function is to use the module definition file (. def), Visual C ++ provides a more concise and convenient method, that is, the "_ declspec ()" keyword is followed by "dllexport", telling the connection to export this function, for example: _ declspec (dllexport) int _ stdcall myexportfunction (INT itest); Put "_ declspec (dllexport)" at the beginning of the function declaration, the DLL generated by the connection will export the function [email = "_ myexportfunction @ 4]" _ myexportfunction @ 4 [/Email] ".
The name of the export function above may not be what I want. We want to export the original "myexportfunction ". Fortunately, VC provides a pre-processing indicator "# pragma" to specify the connection options (not only this function, but also many indication functions), as follows:
# Pragma comment (linker, "/export: myexportfunction = _ myexportfunction @ 4 ")
This is just like the hope of the day :). If you want to specify the export sequence, or export the function as the serial number without entryname, this preprocessing indicator (specifically the connector) can be implemented. Let's take a look at the msdn syntax description:
/Export: entryname [, @ ordinal [, noname] [, Data]
@ Ordinal specifies the order. noname indicates that only the function is exported as the serial number. The data keyword specifies that the export item is a data item.

(9)EachItemsCompilePrograms can use# PragmaCommand activation orFinalStopThis compilationProgram SupportCompileFunction.
For example, loop optimization:
# Pragma loop_opt (on) // activate
# Pragma loop_opt (off) // terminate
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-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.
A very important parameter
# Pragma pack (N)
Data boundary alignment:
Take the following structure as an example: struct {
Char;
Word B;
Dword c;
Char D;
}
Default structure size in Windows: sizeof (struct) = 4 + 4 + 4 + 4 = 16;
Same as # pragma pack (4)
If it is set to # pragma pack (1), the structure size is: sizeof (struct) = 1 + 2 + 4 + 1 = 8;
If it is set to # pragma pack (2), the structure size is sizeof (struct) = 2 + 2 + 4 + 2 = 10;
When # pragma pack (1): space is saved, but access speed is reduced;
In system communication, such as communication with hardware devices and other operating systems, the consistency between the two parties must be ensured.

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.