# Pragma once
This is a commonly used command. You only need to add this command at the beginning of the header file to ensure that the header file is compiled once.
# Pragma once is used to prevent a header file from being included multiple times, # ifndef, # define, # endif is used to prevent a macro from being defined multiple times. # 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.
# Ifndef, # define, # endif, which is related to the C ++ language. This is a macro definition in the C ++ language. Using the macro definition, you can avoid 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.
# Pragma language symbol string is a character sequence that provides unique compiler instructions and parameters. The numeric symbol (#) must be the first non-blank character in the compilation indicator line. White space characters can be separated by numerical symbols (#) and the word Pragma.
Purpose: 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.