• ### Announcements

#### Archived

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

# #pragma once

## Recommended Posts

krakrazor    122
When is that command used? i have a little idea but i''m not sure... also would it fix the linking error stating that the class was already difened somewhere else?

##### Share on other sites
pjcast    311
i don''t recommend using prgama once for preventing multiple includes; instead, use this format:

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 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 on other sites
SiCrane    11839
Some versions of gcc also implement #pragma once, but it''s considered deprecated.

##### Share on other sites
shurcool    439
I always go...

#pragma once#ifndef __File_H__#define __File_H__// ...#endif
---
shurcooL

[edited by - shurcooL on January 5, 2004 11:24:45 PM]

##### Share on other sites
Doc    586
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 on other sites
shurcool    439
I read somewhere that this is the best way because those compilers that do support #pragma once will compile large projects faster.

---
shurcooL

##### Share on other sites
Xai    1838
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 on other sites
oliii    2196
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 headerINC_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.EndOfLineEnd FunctionFunction  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 ifEnd FunctionFunction 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 = DescrEnd FunctionSub HeaderGuards()	FileDescriptor()	DocName = ActiveDocument.FullName		DocName   = UCase(DocName)	Directory = UCase(Directory)	if InStr(DocName, ".H") <> 0 Then		InsertHeaderGuards()	End IfEnd 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 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 on other sites
OrangyTang    1298
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 on other sites
shurcool    439
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 on other sites
Doc    586
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?

The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

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