The difference between #pragma once and #ifdef

Source: Internet
Author: User

When a project is relatively large, it is often a sub-file, it is possible to accidentally put the same header file include multiple times, or the head file nested contains:

The A.H contains b.h:

#include "B.h"


The b.h contains a.h:

#include "A.h"

Use one of the header files in main.c:

#include "a.h" int main () {return 0;}

Compiling the above example, the following error will appear:




In order to avoid the same file being include multiple times, there are two ways in C/s + +, one is #ifndef way, and the other is #pragma once way. There is not much difference between the two compilers that can support both, but there are still some subtle differences.


Way One:

#ifndef __somefile_h__#define __somefile_h__//Declaration, definition statement #endif


Way two:

#pragma once//declaration, definition statement


The way #ifndef is supported by the C + + language standard. It does not only guarantee that the same file will not be included more than once, but also that two files (or snippets) with exactly the same content will not be included accidentally.


Of course, the downside is that if the macro names in different header files are accidentally "crashed", it may cause you to see that the header file exists, and the compiler insists it cannot find the state of the statement-a situation that is sometimes maddening, as follows:

A.H macros in the file #ifndef are __a_h__

#ifndef __a_h__#define __a_h__#include "B.h" #endif

b.h Filethe macros in the #ifndef are also __a_h__

#ifndef __a_h__#define __a_h__#include "A.h" #endif


The result compiles the same error, and when the project is big, the error is not easy to find.


Because the compiler needs to open the header file each time to determine if there is a duplicate definition, Ifndef makes the compilation time relatively long when compiling large projects, so some compilers gradually begin to support the way #pragma once.


#pragma once is generally guaranteed by the compiler: the same file will not be included multiple times . Note that the "same file" here refers to a physical file, not two files of the same content. You cannot make a pragma once declaration for a piece of code in a header file, but only for a file. The advantage is that you don't have to bother thinking about a macro name, and of course there's no strange problem with the macro name collision. The compilation speed of large projects has thus improved a bit.


The disadvantage is that if a header file has multiple copies, this method does not guarantee that they will not be included repeatedly. Of course, this repetition is easy to detect and correct, compared to the "no claims" issue caused by the macro name collision.


#ifndef受C the support of the/c++ language standard, which is not limited by the compiler, and the #pragma once method is not supported by some older compilers, and some supported compilers intend to remove it, so it may not be as good as compatibility.


Also see a usage that puts the two together:

#pragma once#ifndef __somefile_h__#define __somefile_h__//declaration, definition statement #endif

It seems to have the advantage of both. However, as long as the use of #ifndef will be the danger of macro name conflict, and can not avoid support #pragma once compiler error, so mixing the two methods does not seem to bring more benefits, it will make some unfamiliar people feel confused.


This article transferred from: Http://blog.csdn.net/jfkidear

The difference between #pragma once and #ifdef

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.