# Difference between pragma once and # ifndef

Source: Internet
Author: User

 

1.

# 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.

 

2.

# 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.

3. Use different methods:

Method 1:

# Ifndef _ SOMEFILE_H __

# Define _ SOMEFILE_H __

... // Some declaration statements

# Endif

 

 

Method 2:

# Pragma once

... // Some declaration statements

 

Method 1 is supported by languages, so the portability is good. method 2 can avoid name conflicts (For details, refer to 4th points)

4.

# 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 pragma once method is generated after # ifndef, so many people may not even have heard of it. At present, it seems that # ifndef is more respected. Because # ifndef is naturally supported by the language and is not limited by the compiler; while # The pragma once method is not supported by earlier versions of compilers. In other words, its compatibility is not good enough. Maybe it's not a problem to wait a few years until the old compiler is dead.

PS: I also see a way to put the two together:

# Pragma once
# Ifndef _ SOMEFILE_H __
# Define _ SOMEFILE_H __
... // Some declaration statements
# Endif

 

It seems that you want to have both advantages. However, if you use # ifndef, there will be a risk of macro name conflicts. Therefore, mixing the two methods does not seem to bring more benefits, but may confuse some unfamiliar people.

 

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.