What is the purpose of #pragma once?
To prevent the same header file from being included multiple times, there are two macro implementations in C/C++: one is the #ifndef method; the other is the #pragma once method.
In compilers that support both methods, there is not much difference between them. However, there are still some subtle differences.
What are the differences in usage between the two?
Example code is as follows:
// Method one: #ifndef __SOMEFILE_H__#define __SOMEFILE_H__ ... ... // Declaration and definition statements#endif
// Method two: #pragma once ... ... // Declaration and definition statements
What are the characteristics of each?
#ifndef
The #ifndef method is supported by the C/C++ language standard. It not only ensures that the same file is not included multiple times but also guarantees that two files (or code snippets) with identical content are not accidentally included simultaneously.
Of course, the downside is that if macro names in different header files accidentally collide, it may lead to situations where the header file exists, but the compiler insists it cannot find the declaration—this can be quite frustrating.
Since the compiler needs to open the header file each time to determine if there are duplicate definitions, using #ifndef can result in longer compilation times for large projects. Therefore, some compilers have gradually begun to support the #pragma once method.
#pragma once
#pragma once is generally guaranteed by the compiler: the same file will not be included multiple times. Note that “the same file” refers to a physical file, not two files with identical content.
You cannot declare a segment of code in a header file with #pragma once; it can only be applied to the file itself.
The benefit is that you no longer have to worry about macro name collisions, and thus strange issues caused by macro name conflicts will not occur. The compilation speed of large projects is also improved somewhat as a result.
The corresponding downside is that if there are multiple copies of a header file, this method cannot guarantee they will not be included multiple times. However, compared to the issues caused by macro name collisions leading to “declaration not found” errors, this type of duplicate inclusion is much easier to detect and fix.
Additionally, this method does not support cross-platform compatibility!
What is the relationship between the two?
The #pragma once method originated after #ifndef, so many people may not have even heard of it. Currently, #ifndef seems to be more favored. This is because #ifndef is supported by the C/C++ language standard and is not subject to any compiler limitations;
whereas the #pragma once method is not supported by some older versions of compilers, and some compilers that do support it plan to remove it, so its compatibility may not be very good.
Generally speaking, when programmers hear this, they tend to choose the #ifndef method, preferring to lower some compilation performance to ensure their code “survives” longer. This is a characteristic of programmers, but that is a side note.
There is also a usage where both methods are combined:
#pragma once#ifndef __SOMEFILE_H__#define __SOMEFILE_H__ ... ... // Declaration and definition statements#endif
Conclusion
It seems to aim to combine the advantages of both. However, as soon as #ifndef is used, there is a risk of macro name collisions, and it cannot avoid errors from compilers that do not support #pragma once, so mixing the two methods does not seem to bring more benefits and may confuse those who are not familiar with them.
The choice of which method to use should be based on an understanding of both methods and the specific situation. As long as there is a reasonable convention to avoid the drawbacks, I believe either method is acceptable. This is no longer the responsibility of the standard or the compiler; it should be resolved by the programmer themselves or by development specifications within a small scope.
To avoid the same file being included multiple times:
-
#ifndef method
-
#pragma once method
In compilers that support both methods, there is not much difference between them, but there are still some subtle differences.

// Method two: #pragma once... ... // Some declaration statements
The #ifndef method relies on macro names not colliding, which not only ensures that the same file is not included multiple times but also guarantees that two files with identical content are not accidentally included simultaneously. Of course, the downside is that if macro names in different header files accidentally collide, it may lead to situations where the header file exists, but the compiler insists it cannot find the declaration.
The #pragma once method is guaranteed by the compiler: the same file will not be included multiple times. Note that “the same file” refers to a physical file, not two files with identical content. The benefit is that you no longer have to struggle to come up with a macro name, and thus strange issues caused by macro name collisions will not occur.
The corresponding downside is that if there are multiple copies of a header file, this method cannot guarantee they will not be included multiple times. However, compared to the issues caused by macro name collisions leading to “declaration not found” errors, duplicate inclusion is easier to detect and fix.
Method one is supported by the language, so it has good portability, while method two can avoid name collisions.
Northwest Old Coder
https://blog.csdn.net/fanyun_01/article/details/77413992