• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Chad Smith

Including Header Files

11 posts in this topic

Ok this is such a basic question and I feel sort of dumb for not remembering.  I know how to work with multiple files, include guards and all that.  That's not the issue.  Actually my question isn't really an issue but more of what is correct.

 

Let's say I have a header file like this

#ifndef FOO_H_
#define FOO_H_
#include <vector>
#include <string>

// the rest of the header file, maybe a Foo Class

#endif

In Foo.cpp when I include Foo.h should I also go ahead and include vector and string or would that just be a waste?

 

Then lets say in another file I will be using the Foo Class.  So I include Foo.h.  Lets say in that file I also need to use vector and string.  If I just include Foo.h then vector and string should still work just fine?  Though should I also go ahead and include vector and string again, just to make things clear or is this waste just slowing down the compile time?

 

Is their a "proper" way?  Or could this just be where precompiled header files could come in handy to speed up the compile time process?  I feel dumb having to ask something this basic and something I work with every day though I'm not sure if I was every taught a "proper" way besides just include guards and all that stuff.

 

Thanks

0

Share this post


Link to post
Share on other sites

If you include Foo.h in another file, then that other file will also include any files included in Foo.h.

 

I don't know if there's a proper way or not, but the only thing i can tell you, is that you don't need to include them again. I think it goes a little into the field of coding style.

Edited by __SKYe
1

Share this post


Link to post
Share on other sites
My general rule is that if I use a class or function in a source file I include the header for the class or function in the source file. Even if it's included already in a different header I include, that header might change later like having an include replaced with a forward declaration.
2

Share this post


Link to post
Share on other sites

Ok, thanks everyone about when to include the other header files.  I knew that it would still be included that I wasn't sure if it was proper to just go ahead and include them again if I needed them.  I will almost try to remember and keep only including the header for the source file if that's the only place I need it.

 

frob: I wasn't entirely sure on the compatibility of #pragma once.  I have heard it is becoming more and more supported on a lot of different compilers.  I know it is of course supported in Visual Studio but out of habit I always did include guards even if my code right now has no point of being compatible with other compilers I think I always did it just in case I did switch my code over to something else.  Hearing that it is supported on more and more compilers brings me to want to start using it now to go ahead and get in the habit of using that also.

 

Also thanks for some issues of include guards.  I knew that the macro has to be unique I guess I never did think of all the different ways that it could end up biting me.

EDIT: Try to rate you up frob for the help but it won't let me right now I guess since I rated the other people up for their quick help and answers.

 

Use multiple header and source files every day in C++, a feature that seems so simple, and you learn something new every day.  Thanks.

 

More of a side question and thought: How many people use the .hpp header file extension besides .h?  I know boost uses .hpp and I've heard that some libraries might use .h if it was orgianlly implemented in C and then if they have it implemented in C++ they'll have another file using the .hpp extension.  I guess I just wondered does anyone go ahead and use the .hpp header extension for their every day personal project use?  Just a side thought that popped in my head for some reason out of the blue.

Edited by Chad Smith
0

Share this post


Link to post
Share on other sites

Should still be able to rate up frob. Each post can have it's own rate from a user. Maybe just a rate flood limit?

 

More of a side question and thought: How many people use the .hpp header file extension besides .h? I know boost uses .hpp and I've heard that some libraries might use .h if it was orgianlly implemented in C and then if they have it implemented in C++ they'll have another file using the .hpp extension. I guess I just wondered does anyone go ahead and use the .hpp header extension for their every day personal project use? Just a side thought that popped in my head for some reason out of the blue.

 

I think this is just a matter of preference, just like using .cc or .cpp for C++ source code.

0

Share this post


Link to post
Share on other sites

Y ea I figured it was just a rate flood limit think.  When I came back on it let me rate him now.

 

Yea, I have a feeling it's just a matter of preference, I guess I'm just more so interested in seeing how many people's personal preference is it to use the .hpp for header files.  I just rarely see it except with boost so it's just a random thought I had really.

0

Share this post


Link to post
Share on other sites

The SFML header includes I use have .hpp in them. It could be handy to use those so they don't accidentally get included in a regular C file/project.

0

Share this post


Link to post
Share on other sites
Personally, if header 'x.h' is used in a file, I #include it, even if I already #included 'x.h' in 'y.h'.
0

Share this post


Link to post
Share on other sites
For me, I #include anything that I use that is not specifically dependent on an already included header... that was worded weird. Let me give an example:
 
Header.hpp
#ifndef HEADER_HPP
#define HEADER_HPP
 
#include <vector>
 
struct MyStruct
{
    std::vector<int> getStuff();
};
#endif
 
Now, if I use vector because of Header.hpp, then I don't #include <vector>:
#include "Header.hpp"
 
// I don't #include <vector> because the only time I'm ever using it is because of
// functions/classes defined in Header.hpp (that is, it's because of Header.hpp I'm
// using a vector here at all, so I don't include <vector> because it's Header.hpp's
// responsibility to include its dependencies)
MyStruct s;
std::vector<int> v = s.getStuff();
 
// That is, MyStruct is defined in Header.hpp, and so is its getStuff() method. If I
// use a vector because of MyStruct::getStuff(), then I don't include the vector
 
However, to continue the example, if I ever use vector in a way that is not associated with Header.hpp, then I #inlcude <vector>
#include "Header.hpp"
#include <vector> // Notice I'm including vector
 
// Here is the same code as before...
MyStruct s;
std::vector<int> v = s.getStuff();
 
// Here, I'm using vector in a way that is not caused by Header.hpp, so I make sure I manually #include <vector>
std::vector<float> other = {1.0};
Note that I also manually include any dependencies if references/pointers are used to non-standard objects (because these things could be forward declared (but you can't (legally) forward declare standard types (except for a few exceptions))). So in the previous example, if getStuff() returned a MyOtherStruct* instead, then I would always include "MyOtherStruct.hpp" (in both code samples) because then Header.hpp can forward declare MyOtherStruct. Edited by Cornstalks
2

Share this post


Link to post
Share on other sites

It's pretty much a stylistic thing, personally I tend to just include to where I don't get any errors for things missing. You of course can be much more proactive about it like making sure you redundantly include headers for things you use in different files even if they're included already in another file.

 

In the end it really doesn't make a huge difference to the end code as long as it functions. More safety is probably better practice since as a few people have mentioned, other header files can be changed that you include, which may suddenly break some of your code or someone else's.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0