Including Header Files

Started by
10 comments, last by Satharis 11 years ago

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

Advertisement

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.

Think of it as your header files are pasted directly where you write your #include "header.h". You can include them again, since those particular files are guarded themselves, but you don't have to.

EDIT/Note: If you don't need the #includes in your header, but do need them in your source file, then only put the #includes in your source file.

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.

As a stylistic thing, the code:

#pragma once

effectively does the same thing, but it adds protection against many common errors. Even though it is not part of the language standard it is supported on basically every compiler that matters to games.

One of the most common problems with include guards that every C or C++ programmer will encounter is that the name on the macro must be unique. It may happen from a copy/paste, it may happen from different libraries that use similar naming conventions, it may happen by shear bad luck, but if you use that method eventually you will be bitten on a project where a header file will mysteriously not be included because the name is not unique.

There are many other issues as well, such as how the macro may get picked up by other segments of code that are not include guards, that include guards require the file to be opened and parsed (although most compilers have optimized this case), that include guards can corrupt other macros, and so on.

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.

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.

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.

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.

Personally, if header 'x.h' is used in a file, I #include it, even if I already #included 'x.h' in 'y.h'.
what

This topic is closed to new replies.

Advertisement