) # Pragma usage

Source: Internet
Author: User

Author: Jeffrey

The Pragma prompt is introduced when ansi c standards are set. This indicator is from Ada. # Pragma is used to prompt the compiler with some information, such as extending a specific function to inline or canceling the boundary check, which is not inherent in C.

Directory:

(0) Preface
(1) # pragma message can output corresponding information in the compilation information output window
(2) # pragma code_seg can be used to set the code segment where function code is stored in the program.
(3) # If Pragma once is used at the beginning of the header file, the header file can be compiled once.
(4) # pragma hdrstop indicates that the pre-compiled header file ends here.
(5) # pragma resource "*. DFM" indicates adding resources in the *. DFM file to the Project
(6) # pragma warning allows you to selectively modify the warning messages of the compiler.
(7) # pragma comment puts a comment record into an object file or executable file
(8) # pragma data_seg create a new data segment and define shared data
Application 1: Define a shared, named data segment in DLL
Application 2: data_seg controls the number of application launches
(9) other usage

(0) Preface

# The Pragma command is used to set the compiler status or instruct the compiler to complete certain 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 paraHere, para is a parameter. Below are some common parameters.

(1) # The pragma message parameter can output the corresponding 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 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:

1 #ifdef   _X86  
2 #pragma message(“_X86 macro activated!”)              
3 #endif 

If _ x86 is defined, "_ x86 macro activated!" is displayed during program compilation !". We won't be scratching our heads because we don't remember some specific macros we defined.

(2) # pragma code_seg can set the code segment where function code is stored in the program,

It is used when the driver is developed. The format is as follows:
# Pragma code_seg ([[{push | Pop},] [identifier,] ["segment-name" [, "segment-class"]);
This command is used to specify the function in. the section in the OBJ file. observe that the OBJ file can use the dumpbin command line program provided by VC. If code_seg does not contain parameters, the function is stored in the OBJ file by default. text section. Push (optional parameter) puts a record into the stack of the internal compiler. An optional parameter can be an identifier or node name pop (optional parameter) to pop up a record from the top of the stack, this record can be an identifier or a node name identifier (optional parameter) an identifier assigned to the record pushed into the stack when the push command is used, when this identifier is deleted, the record in its related stack will pop up the stack "segment-name" (optional parameter), which indicates the node name stored by the function, for example:

1 // by default, functions are stored in. Text.
2 void func1 (){}
3 // store the function in. my_data1
4 # pragma code_seg (". my_data1 ")
5 void func2 (){}
6 // R1 is the identifier and puts the function into section. my_data2
7 # pragma code_seg (push, R1, ". my_data2 ")
8 void func3 (){}
9 int main (){}

(3) # pragma once (commonly used) can ensure that the header file is compiled at the beginning of the header file.

Generally, we only need to include the header file once in the entire project. If there are multiple header files. c /. the CPP file must contain the same header file, such as Windows. h, isn't there two declarations? The traditional method to solve this problem is to define a macro with # define at the beginning of the header file, such as in windows. h:

1 # ifndef _ WINDOWS _
2 # DEFINE _ WINDOWS _
3 # endif

 

This avoids being contained multiple times. But the consequence is that the Code is less readable (personal opinion). VC provides us with another way, that is, to add the following before the file: # pragma once"


(4) # pragma hdrstop indicates that the pre-compiled header file ends here.

The header file is 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.# Pragma package (smart_init ),BCB is 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 allows you to selectively modify the warning messages of the compiler.

The command format is as follows:
# Pragma warning (warning-specifier: Warning-number-list [; warning-specifier: Warning-number-list...])
# Pragma warning (push [, N])
# Pragma warning (POP)
The main warnings are as follows:
Once: only display once (warning/error) Message
Default: resets the warning behavior of the compiler to the default state.
1, 2, 3, 4: four warning levels
Disable: Disable specified warning information
Error: uses the specified warning information as an error report.
 # Pragma warning (Disable: 4507 34; once: 4385; error: 164) is equivalent to: # pragma warning (Disable: 4507 34) // do not display warning messages 4507 and 34

1 # pragma warning (once: 4385) // warning message No. 4385 is reported only once
2 # pragma warning (error: 164) // use the 164 warning message as an error.
3 # pragma warning (push) // Save the existing warning status of all warning information.
4 # pragma warning (push, n) // Save the existing warning status of all warning information and set the global warning level to n. # Pragma warning (POP) // The Last warning message pops up to the stack. All changes made between the inbound and outbound stacks are canceled. Example: # pragma warning (push)
5 # pragma warning (Disable: 4705)
6 # pragma warning (Disable: 4706)
7 # pragma warning (Disable: 4707 )//.
8 # pragma warning (POP) at the end of this Code, re-save all warning information (including 4707, and ).


(7) Pragma comment puts a comment record into an object file or executable file

The command format is: # pragma comment ("comment-type" [, commentstring]) comment-type (comment type ): you can specify one of the five predefined identifiers as follows:
Compiler: puts the version number and name of the compiler into the target file. This comment record is ignored by the compiler. If you provide the commentstring parameter for this record type, the compiler generates a warning. Example: # pragma comment (compiler );
Exestr: Put the commentstring parameter in the executable file when linking. When the operating system loads the executable file, the parameter string will not be loaded into the memory. however, this string can be searched and printed by programs such as dumpbin. You can use this identifier to embed version numbers and other information into the executable file!
Lib: Used to link a library file to the target file. For example, wsock32.lib is used for 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:

1 # pragma comment (Lib, "wsock32.lib ")

Linker: place a link option in the target file. You can use this command to replace the link options passed in by the command line or set in the development environment, you can specify the/include option to forcibly include an object, for example, 1 # pragma comment (linker, "/include :__ mysymbol ")

You can set the following link options/defaultlib/export/include/merge/section in the program. These options are not described here. For details, see msdn!
User: Put the commentstring parameter in the target file to include the commentstring text of the comment. This comment record will be ignored by the linker, for example: 1 # pragma comment (user, "compiled on" _ date _ "at" _ time _) (8) # pragma data_seg create a new data segment and define shared data

Format: # pragma data_seg ("shareddata") hwnd sharedwnd = NULL; // share data
# Pragma data_seg ()

Application 1: Define a shared, named data segment in DLL.
Note: A. global variables in this data segment can be shared by multiple processes. Otherwise, the global variables in the DLL cannot be shared among multiple processes.
B. Shared data must be initialized. Otherwise, the Microsoft compiler will put uninitialized data in the. BSS segment, leading to failure in sharing among multiple processes. Let's say this in a DLL:

1 # pragma data_seg ("mydata ")
2 int g_value; // The global variable is not initialized.
3 # pragma data_seg () // DLL provides two interface functions: int getvalue (){
4 return g_value;
5}
6 void setvalue (int n ){
7 g_value = N;
8}

Then start both processes A and B that call the DLL. If a calls setvalue (5), B then calls int M = getvalue (); the m value is not necessarily 5, but an undefined value. Because the global data in the DLL is private and cannot be shared for every process that calls it. If you initialize g_value, g_value will be put into the mydata segment. In other words, if a calls setvalue (5); B then calls int M = getvalue (); then the value of m must be 5! This enables cross-process data communication!

Application 2: data_seg controls the number of application launches
Sometimes we may want an application to be started only once. Like Singleton, there may be many implementation methods. Here we will talk about how to implement it using # pragma data_seg, it is simple and convenient. Add:

1 # pragma data_seg ("flag_data ")
2 int app_count = 0;
3 # pragma data_seg ()
4 # pragma comment (linker, "/section: flag_data, RWS ")
5 // Add where the program starts
6 if (app_count> 0)
7 // exit the application if the count is greater than 0. {
8 // MessageBox (null, "an application has been started", "warning", mb_ OK );
9 // printf ("No % d Application", app_count );
10 return false;
11}
12 app_count ++; (9) other usage

You can use the # pragma command to activate or terminate some compilation functions supported by the program. For example, loop optimization:

1 # pragma loop_opt (on) // activate
2 # 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: 1 # pragma warns-100
2 // turn off the warning message for warning #100
3 int insert_record (REC * r)
4 {/* function body */}
5 # 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.

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.