Solved

#pragma once?

Posted on 2006-07-07
8
628 Views
Last Modified: 2010-08-05
can anyone tell me why is the #pragma once used so many times in header files? what does it tell the compiler exactly? what happens if i dont use it?
0
Comment
Question by:yattias
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17061347
#pragma once prevents header file to be included more than once to the same source file. In old projects the same was done using:
#ifndef <some unique string>
#define <some unique string>

all h-file contents goes here

#endif

In complicated project with multiple include files there are always cases when the same h-file is included more than once. For example:

// B.h
#include "A.h"

// C.h
#include "A.h"

// D.cpp
#include "B.h"
#include "C.h"

A.h is included twice. #pragma once prevents compilation error (duplicate declaration).
0
 
LVL 15

Expert Comment

by:lakshman_ce
ID: 17061370
Specifies that the header file will be included (opened) only once by the compiler when compiling a source code(.cpp) file.This can reduce build times as the compiler will not open and read the file after the first #include of the module.

If you don't use it everytime you compile .cpp, compiler will open the included .h files to check for the definitions otherwise it would open only if the .h file changes.

0
 
LVL 30

Expert Comment

by:Axter
ID: 17062030
I recommend using header guards over #pragma once, because header guards are portable, and can be used with any compiler, where as the pragma once method only works with VC++ compiler.
#ifndef MY_HEADERNAME_GUARD_
#define MY_HEADERNAME_GUARD_

//.... header content

#endif //MY_HEADERNAME_GUARD_
0
 

Author Comment

by:yattias
ID: 17065432
thanks! but I don't see why would the compiler include a header file more than once and I don't see how I would get a compile error if I don't use #pragma.  any help?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 17065461
// B.h
#include "A.h"

// C.h
#include "A.h"

// D.cpp
#include "B.h"
#include "C.h"

A.h is included twice to D.cpp. For every type which is defined in A.h you will get compilation error without #pragma once.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question