Archived

This topic is now archived and is closed to further replies.

#pragma once

This topic is 5094 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i don''t recommend using prgama once for preventing multiple includes; instead, use this format:

#ifndef MyClassHeader
#define MyClassHeader

All prototypes/classes here

#endif

Also, don''t actually define any global variables within headers... for eg. dont do this in a header:

int r;

instead, if you need to define a global, do this:

extern int r;

That way it is not actually created multiple times.
My two cents...

Share this post


Link to post
Share on other sites
It prevents the file from being included more than once.

Yes, it might fix that error.

Also, it is not supported by all compilers (I think MSVC is the only one that supports it).

Share this post


Link to post
Share on other sites
quote:
Original post by shurcool
I always go...

#pragma once

#ifndef __File_H__
#define __File_H__

// ...

#endif




You''re wasting your time. #ifndef... will always work, thus your #pragma once is redundant and pointless.

Share this post


Link to post
Share on other sites
i will simply say this - #pragma once is the technically better solution - cause it coneys exactly what you want to convey to the compiler, BUT i never use it ... i just use #ifndefs, and they work, and why would you have a LARGE project these days ... use libraries ... every time i''ve seen a large project, it was simply not organized.

Share this post


Link to post
Share on other sites
look for VC6 macros that automatically put header guards in your file. Well, here''s one I got


Name_Of_User = "Olivier renault"

HeaderGuardString = ""

Directory = "
SOURCE" '' the directory for valid header
INC_NAME = "
OLI_" '' the heder guard string extension


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
'' Turn a document filename into an header guard string
''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function MakeHeaderGuards()

''prepare a string to hold the name
HeaderGuardString = "
"

DocName = ActiveDocument.Name
DocName = UCase(DocName)

if InStr(DocName, "
.H") <> 0 Then

''make the complete string with the project name
DocName = INC_NAME + DocName

''scan all the characters in the document name
pos = 1

Do While pos < Len(DocName) + 1

''Get the character at pos
char = Mid(DocName, pos, 1)

''if character is a valid character, copy it
If (char => "
0" And char <= "9") Or (char >= "A" And char <= "Z") Then
HeaderGuardString = HeaderGuardString + char

''else replace it by _
Else
HeaderGuardString = HeaderGuardString + "
_"
End If

''scan next character
pos = pos + 1
Loop

End If

End Function



''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''
'' Put some header guards around an include file
''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GuardInclude()
''DESCRIPTION: Moves cursor to the previous function definition.
ActiveDocument.Selection.FindText "
^#include", dsMatchRegExp
ActiveDocument.Selection.EndOfLine
End Function




Function InsertHeaderGuards()

''make the guard string from the document name
MakeHeaderGuards()

''Look if the guard has already been done
If ActiveDocument.Selection.FindText (HeaderGuardString, dsMatchFromStart + dsMatchWord + dsMatchCase) Then
Exit Function
End If

''add the "
#endif DOCUMENT_H" at end of document
ActiveDocument.Selection.EndOfDocument(False)

ActiveDocument.Selection = vbLf + vbLf + vbLf + "
#endif " + _
HeaderGuardString


''add the "
#ifndef DOCUMENT_H" at start of document
''add the "
#define DOCUMENT_H" at start of document
ActiveDocument.Selection.StartOfDocument (False)

ActiveDocument.Selection = "
#ifndef " + HeaderGuardString + _
vbLf + "
#define " + HeaderGuardString + vbLf + vbLf + vbLf

''add a few lines that you have to include in your
''files to include Document.h
''
''eg.
''/*
''#include <Engine\Controller\Input.h>
''*/



DocName = ActiveDocument.FullName

IncPath = InStr(DocName, "
source")

if (IncPath = 0) then
IncPath = InStr(DocName, "
Source")
end if

if (IncPath <> 0) then

IncPath = IncPath+Len("
Source/")

IncludePath = Mid(DocName, IncPath)


ActiveDocument.Selection = "
/*" + vbLf + _
"#include <" + IncludePath + ">" + vbLf + _
"*/
" + vbLf + vbLf


else
ShortName = "
""" + ActiveDocument.Name + """"

ActiveDocument.Selection = "
/*" + vbLf + _
"#include "+ ShortName + vbLf + _
"*/
" + vbLf + vbLf

end if


End Function



Function FileDescriptor()
''DESCRIPTION: Adds a function definition and performs a outtime include #ifndef step performs no checking on previous runs


''Set the marker that identifies the file description
Descriptor = "
// File : " + ActiveDocument.Name


''Look if the descriptor has already been done
If ActiveDocument.Selection.FindText (Descriptor, dsMatchFromStart + dsMatchWord + dsMatchCase) Then
Exit Function
End If

ActiveDocument.Selection.StartOfDocument (False)

Header = ActiveDocument.Selection
Descr = "/*" + _
vbLf + "------------------------------------------------------------------" + _
vbLf + "File: " + ActiveDocument.Name + _
vbLf + "Started: " + CStr(Now) + _
vbLf + " " + _
vbLf + "$Header: $" + _
vbLf + "$Revision: $" + _
vbLf + "$Locker: $" + _
vbLf + "$Date: $"+ _
vbLf + " " + _
vbLf + "Author: " + Name_Of_User + _
vbLf + "------------------------------------------------------------------" + _
vbLf + "Module: " + _
vbLf + "Description: " + vBLf + _
"------------------------------------------------------------------" + _
vbLf + "$History: $" + vBLf + _
"------------------------------------------------------------------" + _
vbLf + "*/
" + vbLf + vbLf

ActiveDocument.Selection = Descr

End Function


Sub HeaderGuards()

FileDescriptor()

DocName = ActiveDocument.FullName

DocName = UCase(DocName)
Directory = UCase(Directory)

if InStr(DocName, "
.H") <> 0 Then
InsertHeaderGuards()
End If

End Sub


All files have to be listed under a directory called "source".
it''s a bit shitty because I don''t know much about writing VB macros, but you get the idea. I''m sure there are some available on the web.

Bind one of these to a keyboard shortcut, and you''re sorted.

Share this post


Link to post
Share on other sites
quote:
Original post by Xai
i will simply say this - #pragma once is the technically better solution - cause it coneys exactly what you want to convey to the compiler, BUT i never use it ... i just use #ifndefs, and they work, and why would you have a LARGE project these days ... use libraries ... every time i''ve seen a large project, it was simply not organized.


Yeah, it seems like include-guarding your files is such an incredibly common thing to do (I don''t think I have ever not include-guarded a .h file), that it would really be optimal for compilers to support #pragma once. The #ifndef thing can cause problems with name-conflicts, not to mention that it is just plain ugly.

Share this post


Link to post
Share on other sites
quote:
Original post by Doc
You''re wasting your time. #ifndef... will always work, thus your #pragma once is redundant and pointless.


Include guards will always work and so are recommended, but adding #pragma is a good idea as well.

Include guards require the compiler to open the file, parse the guards, then skip over the contents. By comparison the #pragma method tells the compiler exactly what you want - that the file is to be included once and it does not need to be reopened.

This can have a significant improvement on compile times since less files need to be opened when not needed. By using both methods it is robust on different compilers (though the include guards) and faster to compile on those that support #pragma once.

Share this post


Link to post
Share on other sites
quote:
Original post by OrangyTang
quote:
Original post by Doc
You''re wasting your time. #ifndef... will always work, thus your #pragma once is redundant and pointless.


Include guards will always work and so are recommended, but adding #pragma is a good idea as well.

Include guards require the compiler to open the file, parse the guards, then skip over the contents. By comparison the #pragma method tells the compiler exactly what you want - that the file is to be included once and it does not need to be reopened.

This can have a significant improvement on compile times since less files need to be opened when not needed. By using both methods it is robust on different compilers (though the include guards) and faster to compile on those that support #pragma once.


That''s exactly what I meant (and wanted to say); thanks for supporting me.

---
shurcooL`

Share this post


Link to post
Share on other sites
quote:
Original post by OrangyTang
Include guards require the compiler to open the file, parse the guards, then skip over the contents. By comparison the #pragma method tells the compiler exactly what you want - that the file is to be included once and it does not need to be reopened.

This can have a significant improvement on compile times since less files need to be opened when not needed. By using both methods it is robust on different compilers (though the include guards) and faster to compile on those that support #pragma once.


Meh. You could reasonably easily implement a file cache and include guard detection method into the compiler. #pragma once is a cop-out.
I still reckon the whole #pragma once thing is a waste of time. I mean, really, how slow is your computer that you notice the time it takes to open a file several times? The overwhelming majority of time taken is the actual compilation.
And if your include file is being opened so frequently, shouldn''t the OS cache it anyway?

Image loads when I''m online since I''m too lazy to find a permanent host.The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Here''s an excerpt from our intranet C++ coding guidelines on include guards:

----------------------------
External include guards exist to stop the pre-processor opening up Header.h, scanning the file in its entirety, and returning only to find that the file never needed to be opened in the first place. This can affect compile speeds dramatically.

It has been found that the use of #pragma once, on compilers that support it, does not replace the functionality of external include guards. Experiments with the latest VC7.1 compiler show that #pragma once is a lot slower than external include guards. See the following page for details of the experiment:

http://www.eskimo.com/~hottub/software/include/

The results obtained by putting #pragma once in the include guard test were as follows:

* External Include Guards: 0.08s
* Internal Include Guards: 6.25s
* No Include Guards: 9.70s

Note that external include guards are not needed when including from .cpp files.
----------------------------

Bottom line: #pragma once is useless.

Beside that point, there''s been interesting discussion on the gcc compiler mailing list about whether the person writing the header file should be allowed to dictate the use of their header file. This was the reason #pragma once or something similar was deprecated - gcc now handles all this automatically if you have include guards (I think, anyway).

Share this post


Link to post
Share on other sites