Circular Depandancies

Started by
14 comments, last by Mr Lane 20 years, 3 months ago
Inclusion Guards are just the beginning...not the end. There are lots of other problems. Here is another rule of thumb.

Don''t put #include statements in your header unless absolutely necessary. Put them in your .cpp files when possible. Predeclare a class instead of #including it in a header if you can.

To really solve the problem you have to understand the problem. The preprocessor replaces every #include with the contents of the file that you list. Consider FileA.h that includes FileB.h and visa-versa. Understanding this problem is the beginning of understanding the extent of the problem.

The following is a slightly complex solution that comes from lots of experience:
For each group of related files create two additional header files for the group. I will use the example of all Graphics files. Name them InternalGraphics.h and ExternalGraphics.h. Inside your group ONLY include your pch file and InternalGraphics.h (stdafx.h is a pch, if you don''t understand, don''t use one). Outside your group don''t include the class header files, ONLY include ExternalGraphics.h. Organize these two files so that they predeclare classes and then include the files in the correct order.

This solution is obviously overkill for small projects. But it''s a lifesaver for large ones.


// Example InternalGraphics.h
// This file contains ALL files necessary for ANY
// .cpp file in the Graphics Module

#include <list>
#include <map>
#include <math.h>
#include <float.h>
#include <DirectX.h>

// Header for another module
// Graphics Module needs to use parts of this module
#include "ExternalResourceLoader.h"

class VertexData;
class PixelShader;
class GraphicsEngine;

#include "PixelShader.h"
#include "VertexData.h"
#include "GraphicsInterface.h"
#include "GraphicsEngine.h"

// End InternalGraphics.h



// Example ExternalGraphics.h
// This file ONLY contains includes necessary for other
// modules to use the graphics engine

#include <list>

#include "GraphicsInterface.h"

// End ExternalGraphics.h





-----------
VenDrake

"My stupid jar is full. I can''t talk to you anymore."
-------------VenDrakeTo understand recursion, you must first understand recursion.
Advertisement
quote:Original post by Mr Lane
I was also taught that extern actually does nothing as all functions and global variables are globals in C anyway, unless declared static, making them local to the source file...


Whoever taught you that has NO idea what extern does. Extern informs the compiler that the variable is defined in another module, and therefore can''t be resolved until link time.

For more information, google for "separate compilation" or "extern keyword".
quote:Original post by Anthony Serrano
quote:Original post by Mr Lane
I was also taught that extern actually does nothing as all functions and global variables are globals in C anyway, unless declared static, making them local to the source file...


Whoever taught you that has NO idea what extern does. Extern informs the compiler that the variable is defined in another module, and therefore can''t be resolved until link time.

For more information, google for "separate compilation" or "extern keyword".


Yes but functions and globals are extern by default in C...the extern keyword is merely convention. I doubt Dr. Zoltan Symogi the programming freak at my university is wrong...but then again it might be one to check.
quote:Original post by pjcast
If you post the exact error messages, it will be easier to help solve your specific error.


The error i have been getting is:

OpenGL.c D:\LANEEN~1\lesson01\OpenGL.o(.bss+0x0)
multiple definition of `hDC''

And this happens for hDC, hRC, hWnd and hInstance, but not any of the other variables. The only way i have found to solve it is to take it out of the header, put it in the OpenGL.c file and declare them static...but of couse this means that i cant use them anywhere else. I suppose I can deal with that, but its annoying. What is the problem here?
What compiler/linker or IDE is that error message from? Dev-cpp? Just curious...

Well, I can't really tell why it says it's defined more then once. If you only defined it once in your .c file. Perhaps it was defined again in one of the modules that gets linked in.

One suggestion, when you make a global variable, you run the risk of it being declared in other places. And with the generic nature of the variable names (hInstance, hWnd, etc...) there is a big risk that someone else may have used those same names somewhere else.

I would say use namespaces, but it's apparent your using c. SO, you should try changing thier names to something more specific to your app. For example, change hInstance to g_MyGLWinInstance...

Also, you have included both gl.h and glu.h, now I'm not positive whats in both those headers... But do you really need both in the header? I see your using some GL datatypes in the header, but are they defined in both the headers? Or can you remove at least one header to the source file? Just a question.

Edited - Spelling

[edited by - pjcast on January 13, 2004 2:32:54 PM]
quote:Original post by Mr Lane
Yes but functions and globals are extern by default in C...the extern keyword is merely convention. I doubt Dr. Zoltan Symogi the programming freak at my university is wrong...but then again it might be one to check.


Just did check. Only function prototypes are extern by default; external variables MUST be explicitly externed.

This topic is closed to new replies.

Advertisement