# Pragma once and # If! Defined separated

Source: Internet
Author: User

First, reference a section:


_ Msc_ver

Define the version of the compiler. The following are the _ msc_ver values of some compiler versions (see references 2 in extended reading)

Ms vc ++ 9.0 _ msc_ver = 1500

Ms vc ++ 8.0 _ msc_ver = 1400

Ms vc ++ 7.1 _ msc_ver = 1310

Ms vc ++ 7.0 _ msc_ver = 1300

Ms vc ++ 6.0 _ msc_ver = 1200

Ms vc ++ 5.0 _ msc_ver = 1100

Among them, Ms VC ++ 9.0 is Visual C ++ 2008, ms vc ++ 8.0 is Visual C ++ 2005. For the relationship between version names, see the reference 1 link in extended reading.

Haha, reference againSection


(From Wikipedia)


Using # pragma once instead of include will speed up compilation, because this is a high-level mechanism; the compiler will automatically compare the file name or inode (Chinese is the index node, I personally think it is the path) you do not need to determine # ifndef and # endif in the header file.

However, such high-level management is both good and bad; designers must rely on the Correct Compiler management # pragma once. If the compiler makes a mistake, for example, it does not recognize two different Symbolic Link name indicators in the same file, the compilation will be wrong.

Next question


Today, when we upgraded a VC ++ project using vs 2008, we suddenly found that the project was originally as follows:CodeOf:

# If! Defined (afx_pageperiodtag_h000058ca60b2_438a_11d2_a4e9_00a02474cbe50000included _)

# Define afx_pageperiodtag_h1_58ca60b2_438a_11d2_a4e9_00a02474cbe51_encoded _

# If _ msc_ver> = 1000

# Pragma once

# Endif // _ msc_ver >=1000

When I used the Class Wizard to generate the header file, I found that only the corresponding part of the header file is generated:

# Pragma once

It seems

# Pragma onceIs in VC ++ 5.0

Later, why not add the version above 1000.
It should be added to be compatible with earlier compilers.

Then I tried it specially in VC 6.0. As a result, the code above was generated by Vs in the early stage. It seems that 2008 does not need to use the old mechanism, maybe 2008 thinks they have done enough perfection to ensure that they will not make mistakes when repeatedly including the system's header files during compilation (what I think ).

But pay attention to the following:

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

Therefore, when you have multiple projects in your project, or you need to reference and copy the same header file in other cases, that is, there are two identical header files.# If! Defined +
# Pragma onceIt will be a good choice.

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.