# Pragma usage

Source: Internet
Author: User
# Pragma preprocessing commands are detailed in all preprocessing commands. # The Pragma command may be the most complex, it is used to set the status of the compiler 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, where para is a parameter. Below are some common parameters. (1) Message parameter. The message parameter can output the corresponding information in the compilation information output window, which is very important for controlling the source code information. The method is as follows: # pragma message ("message text") prints the message text in the compilation output window when the compiler encounters this instruction. 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 if we have defined the _ x86 macro in the source code. We can use the following method # ifdef _ x86 # pragma message ("_ x86 macro activated !") # Endif when we define the _ x86 macro, the application will display "_ x86 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. The format is as follows: # pragma code_seg (["section-name" [, "section-class"]). It can set the code segment for storing function code in the program, we use the driver when developing it. (3) # pragma once (commonly used) as long as this command is added at the beginning of the header file, the header file can be compiled once. This command is actually available in vc6, however, considering the compatibility, It is not used much. # Pragma once is related to compilation, that is, it can be used in this compilation system, but not necessarily in other compilation systems, that is, it has poor portability, but now, basically every compiler has this definition. (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 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 to: # pragma warning (Disable: 4507 34) // do not display warning messages 4507 and 34 # pragma warning (once: 4385) // The warning message No. 4385 is reported only once # pragma warning (error: 164) // The warning message No. 164 is regarded 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. Example: # pragma warning (push) # pragma warning (Disable: 4705) # pragma warning (Disable: 4706) # pragma warning (Disable: 4707) //....... # pragma warning (POP) resaves all warning information (including 4707, and) at the end of this Code ). (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.
Each compilation program can use the # pragma command to activate or terminate some compilation functions supported by the Compilation Program. For example, for loop optimization: # pragma loop_opt (on) // activation # pragma loop_opt (off) // termination sometimes, some functions in the program will give the compiler 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 function will generate a warning message with a unique signature 100, this 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. # Pragma pack (N) and # pragma pack () struct sample {char a; Double B ;}; when the sample structure is not added with # pragma pack (N, the sample is aligned according to the largest member. (the so-called alignment refers to alignment for each member when the number of alignment is N, if the size of member A is smaller than N, A is expanded to n. If the size of member A is greater than N, A is used.) Therefore, the size of the structure above is 16 bytes. when the sample structure is added with # pragma pack (1), sizeof (sample) = 9 bytes; no NULL bytes. (Note: WHEN n is greater than the maximum member size of the sample structure, n is the maximum member size. So when n is larger, the faster the structure, the larger the size; otherwise) # pragma pack () means to cancel # pragma pack (n, that is to say, the subsequent structure does not need # pragma pack (N)

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.