?
Solved

Clarification on #pragma once

Posted on 2001-07-30
8
Medium Priority
?
437 Views
Last Modified: 2012-05-04
Just need something clarified...

#pragma once

Am I correct in assuming that the purpose of this
command is to physically allow a file to only be
opened once during compilation, in order to prevent
the same file from accidentally being compiled more
than once?

Thanks
    -Chris

0
Comment
Question by:CPOsosky
[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
 

Expert Comment

by:ckcheng091799
ID: 6336490
<!-- From MSDN -->

#pragma once

Specifies that the file, in which the pragma resides, will be included (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 would follow:

0
 

Author Comment

by:CPOsosky
ID: 6336550
Thats nice.
Thats the same thing I found in the MSDN.
Now hows about answering my question? :) hmmmmmmmm?

-Chris
0
 
LVL 6

Expert Comment

by:snoegler
ID: 6336744
Hmmmmmmmmm ?
The wording is pretty clear :)
What else do you expect? "I swear, it is *really* opened only once!"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:snoegler
ID: 6336748
The main purpose of "#pragma once" is to allow to put code in the header file which otherwise would cause the linker to complain about doubly defined symbols, like:

// test.h

char szTest[] = "HALLO";

If this header is included twice, the linker will complain. It will not complain if you'd used "#pragma once".
0
 
LVL 22

Accepted Solution

by:
nietod earned 200 total points
ID: 6337121
Since header files may contain definitions for things that cannot be externally defined and whose definitions may appear only 1 time in a transalation unit, like for example enums.  You often need to make sure that a header file's contents are included only one time in a translation unit.   The standard ways to do this is to enclose the contents of the file in an "include gaurd", which is a pre-processor #ifndef..#endif construct that checks the existance of a #defined value that the header itself defines.  Thus the first time the header file is included, the contents of the #if are included and the value is #defined.  Then the next time the header file is included, the contents of the #if are not included since the value has already been #defined.

For example

#ifndef  MMYHEADER_H
#define MYHEADER_H

enum Days
{
   Monday, Tuesday, Wednessay, Thursday, Friday, Saturday, Sunday
};
#endif

The VC #pragma once accomplishes this same behevior, but without your having to use the include gaurd.   Its veyr handy, but its also veyr VC-speciic.  If you will be porting your code to other compilers, you need to be aware that they will probably not support it.  In that case, you will not want to rely on this feature.

0
 
LVL 22

Expert Comment

by:nietod
ID: 6337135
>> The main purpose of "#pragma once" is to allow to put code in the header file which otherwise would
>> cause the linker to complain about doubly defined symbols, like:
That is a common missunderstanding.   It will not acheive that.  it will prevent the compiler from complaining about a duplicate symbol.   In this case if the header was included two times in the translation unit the szTest variable would be defined twice and would cause a compiler error. Using either an include gaurd or the once #pragma will prevent the compiler error, but that is the wrong solution.  it then sets ou up for the linker error.    The problem is that that variable is defined with external linkage.  And it is defined in that inclyude file.  If that include file is included into 2 or more translation units, then each translation unit will define that varaible.  Thus when they are linked together the linker will complain about duplicate definitions.  

If things have internal linkage, then you can use include gaurds or #pragma once to prevent duplicate deffinitons.   If they  have external linkage you cannot do that (sometimes it might work, but in general it won't).  Instead you mist never place a definition for somethign with external linkage in an include file.  The declaration goes in the include file and the definition goes into one source code file.  
0
 
LVL 6

Expert Comment

by:snoegler
ID: 6339667
Yes you're right (of course :-)

I haven't used #pragma once ever, and to be honest i can't understand why it works like it works.

If i got that right, you can accomplish #pragma once functionality by embracing the whole file with #ifndef XYZ, #define XYZ / #endif ?
If it is so, its only purpose is speeding up the compiling process?
And then: Couldn't this optimization be done by the compiler using a map of already included headers? If so, the whole #pragma once construct is a non-portable replacement for the #ifdef #define #endif block?
0
 
LVL 22

Expert Comment

by:nietod
ID: 6339920
>> If i got that right, you can accomplish #pragma once functionality by
>> embracing the whole file with #ifndef XYZ, #define XYZ / #endif ?
right its a substitute for that more standard (and completely portable) technique.

>> If it is so, its only purpose is speeding up the compiling process?
Yes.  Its also more convenient than writting the $ifdef...#endif.    But its not portable to other compilers.

>> Couldn't this optimization be done by the compiler using
>> a map of already included headers?
I'm sure it is done that way, althougn not necessarily with a map.  
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

777 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