Public Group

# A question about .h and cpp files.

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

## Recommended Posts

Hemm... I was just wondering. Was looking thru two books of mine and there source code. One is a bit older and that I think has a newer version out. The way they have .h files and there corrisponding .cpp files is a bit different. One had #pragma once and included .h files (Like iostream) with the .h file. While the other had the whole #ifndef, #define setup and all includes where with the .cpp file. The second one is the more accepted practice right?

##### Share on other sites
If I'm not totally mislead here, #pragma once was introduced by Microsoft. GCC also declared support for it in the meantime, but it's not a part of the C++ standard.

The catch is, #ifndef .. #endif include guards require the compiler to actually open the file and run it through the preprocessor, while #pragma once is supposed to make the compiler insert the header's filename in its internal already-included list so it can just skip the entire file if it sees it included somewhere else. In larger projects with many small headers, this can be a substantial improvement in compiling speed.

Some OS projects actually use both, so it works on a plain standards conforming compiler and is optimized in compilers with #pragma once support.

-Markus-

##### Share on other sites
Well if I knew the best way to put in both I would. But I guess ill just stay with the more standard #ifndef for now.

##### Share on other sites
Here is an intresting read on this matter as well. If I am not mistaken either, you can achieve the functionality of #pragma once with #ifndefs if you structure them right. Basically you will be using them in the .h and .cpp files. You will use them normally in the .h files but in the .cpp files, you use include gaurds on the lines that include files.

Here's a quick example:
//Main.h#ifndef _MAIN_#define _MAIN_...#endif//Main.cpp#ifndef _MAIN_#include "Main.h"#endif...

You basically will follow that structure for the respective files. I do not know how well this works for all projects though. I think I had tried in on a rather large fun project I had and a few dependices were mixed up, so I do not know if it was me or the way thie method works. I can't remember the exact source I read this at, but it had to do with gaming.

- Drew

##### Share on other sites
ohh one thing I forgot to ask explicidly about is where is it best to have other includes. In the .h file or the .cpp file?

Ex 1. includes in the .h file
//.h
#include <iostream>
code...

//.cpp
#include ".h"
code...

Ex 2. all include in the .cpp file
//.h
code...

//.cpp
#include <iostream>
#include ".h"
code...

##### Share on other sites
That is a question that depends on your project. I've heard both sides of the argument that you are not supposed to include header files inside header files and you are. I can't really comment which one is better. What I do is that if I need a header file in another header file, I do that, if not, then I don't. I'm sure you will get a better response taking either side. I'm more netural on this topic. Really though, there is no 'best' way that fits all [smile].

- Drew

##### Share on other sites
I try to put as many includes in the CPP as possible, because it prevents major cascading changes when headers modify other headers, and so on.

##### Share on other sites
Hemm... then I guess ill just have to mess around then. Find what works best for me.

##### Share on other sites
In header files, do include any headers that are required just for the stuff in the header itself. (If you put those includes in the cpp, you would have to remember to include them before the header itself, and could run into a problem with that due to circular dependency. This is exactly what the include guards are there for - to allow you to do the inclusion you need like this.) For other headers, it's a judgement call, but you will probably want to include most of them in the .cpp.

##### Share on other sites
.h file -> C/C++ Header File
.cpp file -> C++ Source File

• 32
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631349
• Total Posts
2999473
×