# Difference between Pragma once and # ifndef

Source: Internet
Author: User

[Reproduced from: http://www.cppblog.com/szhoftuncun/archive/2007/10/31/35356.html]
# Difference between Pragma once and # ifndef

Specifies that the file will be encoded ded (opened) only once by the compiler when compiling a source code file.

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

Several important Macros in the appendix (msdn ):

>>_Msc_ver
> Defines the compiler version. defined as 1200 for Microsoft Visual C ++ 6.0.> always defined.

>>_Mfc_ver
> Defines the MFC version. defined as 0x0421 for Microsoft Foundation Class> library 4.21. Always defined.

>># Pragma once
> Specifies that the file, in which the Pragma resides, will be encoded 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 wocould follow:

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.