Jump to content
  • Advertisement

Archived

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

krakrazor

#pragma once

This topic is 5308 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

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 this post


Link to post
Share on other sites
Advertisement
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
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 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 read somewhere that this is the best way because those compilers that do support #pragma once will compile large projects faster.

---
shurcooL`

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!