Explanation of C pragma usage and pragma usage
# Pragma 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 Para
Here, 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: 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:
# Ifdef _ X86
# Pragma message ("_ X86 macro activated !")
# 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. Optional parameters can be an identifier or a node name.
Pop (optional parameter) pops 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 that is pushed into the stack when the push command is used. When this identifier is deleted, the record in the stack associated with it will be popped up.
"Segment-name" (optional) indicates the node name stored by the function.
For example:
// By default, functions are stored in. text.
Void func1 () {// stored in. text
}
// Store the function in. my_data1
# Pragma code_seg (". my_data1 ")
Void func2 () {// stored in my_data1
}
// R1 is the identifier. Put the function in. my_data2.
# Pragma code_seg (push, r1, ". my_data2 ")
Void func3 () {// stored in my_data2
}
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:
# Ifndef _ WINDOWS _
# Define _ WINDOWS _
# 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. 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 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)
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.
# 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 puts a comment record into an object file or executable file
The Instruction format is
# Pragma comment ("comment-type" [, commentstring])
Comment-type: You can specify one of the five predefined identifiers. The five predefined identifiers are:
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, if we use WSock32.lib during 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:
# 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:
# Pragma comment (linker, "/include :__ mySymbol ")
You can set the following link options in the program
/DEFAULTLIB
/EXPORT
/INCLUDE
/MERGE
/SECTION
These options are not described here. For details, refer to msdn!
User: Put the commentstring parameter in the target file to include the text information of the comment. This comment record will be ignored by the linker, for example:
# 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:
# Pragma data_seg ("MyData ")
Int g_Value; // The global variable is not initialized.
# Pragma data_seg ()
DLL provides two interface functions:
Int GetValue ()
{
Return g_Value;
}
Void SetValue (int n)
{
G_Value = n;
}
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! # Pragma
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:
# Pragma data_seg ("flag_data ")
Int app_count = 0;
# Pragma data_seg ()
# Pragma comment (linker, "/SECTION: flag_data, RWS ")
Add
If (app_count> 0) // exit the application if the count is greater than 0.
{
// MessageBox (NULL, "an application has been started", "Warning", MB_ OK );
// Printf ("no % d application", app_count );
Return FALSE;
}
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:
# 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.
# Pragma once and # ifndef
To prevent the same file from being included multiple times
1 # ifndef Mode
2 # pragma once Method
There is no big difference between compilers that support these two methods, but there are still some minor differences between the two.
Method 1:
# Ifndef _ SOMEFILE_H __
# Define _ SOMEFILE_H __
... // Some declaration statements
# Endif
Method 2:
# Pragma once
... // Some declaration statements
# The ifndef method depends on the macro name and cannot conflict with each other. This not only ensures that the same file is not included multiple times, but also ensures that two files with identical content are not accidentally included at the same time. Of course, the disadvantage is that if the macro names of different header files are accidentally "crashed", the header files may obviously exist, but the compiler can hardly find the declaration.
# Pragma once is guaranteed by the compiler: the same file will not be contained multiple times. Note that the "same file" here refers to a physical file, not two files with the same content. The advantage is that you don't have to think about a macro name any more. Of course, there won't be any strange problems caused by the macro name collision. The disadvantage is that if a header file has multiple copies, this method cannot ensure that it is not repeatedly included. Of course, repeat inclusion is easier to detect and correct than the "no declaration found" problem caused by macro name collision.
The first method is supported by the language, so the portability is good. The second method can avoid name conflicts.