Public Group

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

## Recommended Posts

Hi guys, this is probably a trivial question but I really appreciate your help. I’m currently going through ‘Beginning OpenGL Game Programming’. Now the book introduces a simply application in C++ in the second chapter that draws a rotating triangle in a window. The code is contained in 3 files:
winmain.cpp
CGfxOpenGL.cpp
CGfxOpenGL.h

Now in winmain.cpp, CGfxOpenGL.h is included like so,
#include 'CGfxOpenGL.h'

and the same line is found in CGfxOpenGL.cpp. CGfxOpenGL.h simply contains the definition of a class called CGfxOpenGL, and then the class functions are defined in CGfxOpenGL.cpp. My question is should the include lines in both .cpp’s for the CGfxOpenGL.h header be wrapped by #ifndef #define #endif:
#ifndef
#define CGFX
#include 'CGfxOpenGL.h'
#endif

Thanks alot guys [smile]

##### Share on other sites
No the header inclusion guard is only put in the header file. In this case in CGfxOpenGL.h should have something like this.

#ifndef CGfxOpenGL_H#define CGfxOpenGL_Hclass CGfxOpenGL{...}#endif

then all cpp files can just include CGfxOpenGL.h as usual.

winmain.cpp
#include "CGfxOpenGL.h".........

##### Share on other sites
Just chiming in. Does anyone know of any caveats in using "#pragma once" for this? Or have I got its intended usage wrong?

##### Share on other sites
Quote:
 Original post by deadstarJust chiming in. Does anyone know of any caveats in using "#pragma once" for this? Or have I got its intended usage wrong?

wikipedia has some interesting things to say about #pragma once. Basically, it's not standard, but it's widely supported and can lead to faster compilation.

##### Share on other sites
ah nhatkthanh thank you. I just remembered as well the books comes with project files, I totally forgot about, I check and in the actual file it is implemented as you say, thanks so much for your help [smile]

and deadstar, I did a quick google, and as far as I can see there's nothing really wrong in using that, but it is no-standard so not actually part of the C++ langauge, it is part of the compiler, so I suppose if you want completely universal code use #ifndef, but I think most modern compilers will support #pragma, but have a little look here some nice info, Thanks guys [smile]

EDIT : Ah Captain P [wink]

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633725
• Total Posts
3013558
×