# 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.
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, where para is a parameter. The following describes some common parameters.
(Each implementation of C and C ++ supports some features unique to its host machine or operating system. some programs, for instance, need to exercise precise control over the memory areas where data is placed or to control the way certain functions receive parameters. the # pragma directives offer a way for each compiler to offer machine-and operating-system-specific features while retaining overall compatibility with the C and C ++ languages. pragmas are machine-or operating-system-specific by definition, and are usually different for every compiler .)
(1) Message parameter. The message parameter is one of my favorite parameters. It can output the corresponding information in the compilation information output window.Source codeInformation control is very important. The usage is as follows:
# Pragma message ("message text"). When the compiler encounters this instruction, it will be removed in the compilation output window.
Text.
When weProgramDefines many macros to control the sourceCodeIn versions, we may forget whether these macros are correctly set. In this case, we can use this command to check the macros during compilation. Suppose we want to determine whether we have defined the _ x86 macro in the source code. We can use the following method:
# Ifdef _ x86
# Pragma message ("_ x86 macro activated !")
# Endif
After we define the _ x86 macro, the application will be displayed in the compilation output window during compilation.
"_ X86 macro activated !". We won't catch ears because we don't remember some specific macros we defined.
Scratching your head.
(2) The other Pragma parameter that is used more frequently is code_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 (commonly used ). You only need to add this command at the beginning of the header file to ensure
The header file is compiled once. This command is actually available in vc6, but it is not used much considering its compatibility.
(4) # pragma hdrstop indicates that the pre-compiled header file ends here, and the subsequent header files are not pre-compiled. BCB
You can pre-compile the header file to speed up the link, but if all the header files are pre-compiled, it may occupy too much disk space, so use this option 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 encoding.
Translation priority. If # pragma package (smart_init) is used, BCB first
After compilation.
(5) # pragma resource "*. DFM" indicates adding resources in the *. DFM file to the project. *. DFM includes Windows
Body appearance definition.
(6) # pragma warning (Disable: 4507 34; once: 4385; error: 164) 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 provides 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 can be executed
File. Common lib keywords can help us to connect to a library file.
(8) # pragma pack () We know that in VC
8-byte alignment. If we do not want to use 8-byte alignment (this is often required when the network is changed), we can add
# Pragma pack (1)
Struct
{
......
}
# Pragma pack ()
Ii. # If _ msc_ver> 1000 # pragma once # endif
(1) _ msc_ver. Defines the compiler version. defined as 1200 for Microsoft Visual C ++ 6.0. Always defined.
(2) # If _ msc_ver> 1000 means that if the VC compiler version is later than 1000, this statement
Compiled! The statement # pragma once is not supported in versions earlier than 1000.
(3) # pragma once. Specifies that the file, in which the Pragma resides,
Will be pinned ded (opened) only once by the compiler in a build. A common use for this Pragma is the following:
// Header. h
# Pragma once
// Your C or C ++ code wo'd follow:
# The first line of The Pragma once header file indicates that this file is only compiled by the compiler file during compilation.
(Open) once! It is generally used in. h to prevent files from being repeatedly included!
Iii. # pragma once and # ifndef # define # endif
(1) from the definition, we can see that the pragmas command is exclusive to a certain machine or operating system, and different compilers often have different features. # Pragma once this is a compiler-related instruction, that is, in this compilation system
It can be used, but it is not fixed in other compilation systems, that is, the porting type is poor. But now, basically
This definition is already available in every compiler.
# Ifndef # define # endif: Language-supported commands, which are macro definitions in C/C ++.
Macro definition avoids multiple compilation of files. Therefore, it is effective in all compilers that support the C ++ language. If the program to be written is cross-platform, it is best to use this method.
(2) # ifndef # define # endif # ifndef has other functions to prevent repeated header file references.
Only one application. # Pragma is only supported by Microsoft.
(3) # ifndef # define # endif he reads # After ifndef has been defined, it will skip
This blockbuster is until # endif. This will increase the build time, because this file will be opened each time compiler searches for the entire file. If you encounter # pragma once, it will immediately stop,
Close the opened file. To some extent, the build time is reduced. General Usage:
# Ifndef
# Define
# Pragma once
.....
# Endif
Iv. # pragma data_seg (". mdata")... # pragma data_seg () allows the compiler
Put all initialized variables in a new. mdata segment. One application is a single application.
Sometimes we may want an application to be started only once. Like Singleton, there may be many implementation methods. Here we will talk about the implementation of # 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 ++;