#Include Problem

Started by
3 comments, last by certaintragedy 18 years ago
Hi, I have a problem in joining the files of my game engine together. I need to have "Engine.h" which will include all of the DirectX headers, stdio.h, math.h, etc. and also the other subsystem's headers, e.g. Renderer.h, DebugFile.h. Well, I guesss code is better than words. DebugFile.h

#pragma once

class CDebugFile
{

// this class uses FILE, fopen, fclose and such.

}

Engine.h


#pragma once

#include "stdio.h"
#include "stdlib.h"

#include "DebugFile.h"

class CEngine
{

// blah

};

Does anyone know why the compiler would claim to not being able to find the FILE type and the file functions? As I understand it, DebugFile.h should be included after stdio.h, and so it should be able to find them. I have no idea what's going on. If it makes any difference, the implementation of CDebugFile is actually contained in DebugFile.cpp, which #includes its header file. Maybe it's something to do with the #pragma once command? Any help is greatly appreciated, thanks. :) James.
Advertisement
Perhaps you should be #includeing <cstdio> in DebugFile.h -- id est, where it's needed?


jfl.
The error may come from DebugFile.cpp as it probably doesn't include stdio before including its own header. But as the previous poster said: it's a good idea to make header files 'self supporting'. That is: include everything you need to let the header file pass. Don't include more than necessary. And use forward declarations where possible.

Illco
If DebugFile.h needs stdio.h, then obviously you should include it in DebugFile.h. Don't rely on it being included elsewhere -- that is a mistake.

Here are some good rules for header files:
  • Don't use a header file that includes all the other header files (except when using pre-compiled headers). It will lead to problems.
  • Header files should be self-sufficient. That is, they include whatever files they need in order to compile.
  • Every file (both header and source files) should include exactly the files that they need -- no more, no less. Strict adherence to this is time-consuming, so laziness is reasonable (up to a point).
  • In header files, use forward declarations whenever possible.
  • The first file included in a source file should be its header file (except when using pre-compiled header files). This helps ensure that header files are self-sufficient. Note: some Windows header files (such the Win32 and the OpenGL headers) are not self-sufficient, so you have to work around them.
Also, read this: Organizing Code Files in C and C++
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
To extend a little on what Illco said: The compiler processes each CPP file individually. So, if you only have

In DebugFile.cpp:
#include "DebugFile.h"...


Then, when the compiler begins compiling DebugFile.cpp THE ONLY INCLUDES IT SEES are those expressly stated in DebugFile.h, regardless of whether Engine.h is included by some other CPP file.

Remember, the compiler "starts fresh" in terms of includes for each CPP file in your project, so trace through your files from top to bottom and follow where the compiler goes as it hits each #include. Unless you've explicity stated either in the CPP file or in the files that it #includes that you want to #include some header, the compiler will not do it, and this appears to be the case with your example (although I'm only speculating without being able to see the DebugFile.cpp code).

This topic is closed to new replies.

Advertisement