Jump to content
  • Advertisement
Sign in to follow this  
Genesiiis

Header Double Include?

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

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


Link to post
Share on other sites
Advertisement
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_H

class CGfxOpenGL
{
...
}

#endif





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

winmain.cpp


#include "CGfxOpenGL.h"

....
...
..




Share this post


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


Link to post
Share on other sites
Quote:
Original post by deadstar
Just 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 this post


Link to post
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!