# Pragma once and # ifndef Parsing

Source: Internet
Author: User

To prevent the same file from being included multiple times, there are two methods in C/C ++: # ifndef and # pragma once. 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 __
...... // Declare and define statements
# Endif

Method 2:

# Pragma once
...... // Declare and define statements

# The ifndef method is supported by the C/C ++ language standard. It not only ensures that the same file will not be contained multiple times, but also ensures that the two files with the same content (orCodeSegment) is not accidentally included at the same time.
Of course, the disadvantage is that if the macro names in different header files are accidentally "crashed", it may cause you to see that the header file exists clearly, the compiler makes it hard to say that the declaration cannot be found-this situation is sometimes very frustrating.
Since the compiler needs to open the header file every time to determine whether there are repeated definitions, ifndef will make the compilation time relatively long during compilation of large projects, therefore, some compilers gradually begin to support the # pragma once method.

# Pragma once is generally 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. You cannot make a pragma once Declaration for a piece of code in a header file, but only for the file.
The advantage is that you don't have to bother thinking about a macro name. Of course, there won't be any strange problems caused by the macro name collision. The Compilation speed of large projects is also improved.
The disadvantage is that if a header file has multiple copies, this method cannot ensure that it is not repeatedly included. Of course, compared with the "no declaration found" issue caused by macro name collision, such repetitive inclusion can be easily found and corrected.

# 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. # Ifndef is supported by C/C ++ language standards and is not limited by the compiler. # pragma once mode is not supported by earlier versions of compilers, some supported compilers intend to remove it, so its compatibility may not be good enough. Generally, whenProgramIf you hear this, you will always choose the # ifndef method. In order to make your code "alive" for a longer time, you usually prefer to reduce some compilation performance. This is the programmer's personality. Of course, this is a digress.

You can also see that the two are put together:

# Pragma once
# Ifndef _ somefile_h __
# DEFINE _ somefile_h __
...... // Declare and define 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, and it cannot be avoided that the # pragma once compiler reports errors. Therefore, mixing the two methods does not seem to bring more benefits, it will confuse some unfamiliar people.

The method you choose depends on the actual situation when you know the two methods. As long as there is a reasonable agreement to avoid disadvantages, I think either method is acceptable. This is no longer the responsibility of the standard or compiler. It should be done by the programmer himself or a small scope of development specifications.

BTW: some discussions on GNU seem to be intended in GCC 3.4 (and later ?) # Pragma once is supported. But in fact, the GCC 3.4.2 and GCC 4.1.1 In my hands still support # pragma once, even without deprecation warning, but gcc2.95 will propose a warning for # pragma once.
vc6 and later versions also provide support for the # pragma once method, which should be basically stable.

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.