Weird header linking in c++

Started by
4 comments, last by Zahlman 18 years, 5 months ago
In my description below "->" means "needs": Engine.h -> Events.h -> Player.h -> Objects.h -> Globals.h Engine.h needs every other headers and Events.h needs the 3 last headers and so on... The problem is that i have Engine.cpp + Events.cpp + Player.cpp + Objects.cpp That needs only the header with the same name as itself (without the suffix .cpp :) ) Problem: When the .cpp files includes their .h files they also includes the other .h , and the "#ifndef" (as I use correctly in each .h file) doesn't work correctly when there is other .cpp files that is including the same headers in the compilation. I encounter the problem in the main.cpp where i define the Engine.h (without it I cannot run the game functions as I'am calling in the main.cpp) (By the way: Im using Dev-C++) Please help! :S
Please comment my english!
Advertisement
It would be nice if you could post the errors you get when compiling. Most likely your problem is that you instance some variable in your header files. Or in other words you have

int i; //this is a global

or something like it in a header file. The way you probably want to fix it is to use extern.

aheader.h:
extern int i;

asinglecppfile.cpp:
int i;

So the extern tells all the cpp files that use that header, that there's a integer i somewhere, and it'll be found during linking. Then you create the actual instance in one of your cpp files.

Another method is to use static int i. But in this case each cpp file has it's own unique integer i. Which I doubt is what you want.
Quote:Original post by Cocalus

<good-stuff />

Another method is to use static int i. But in this case each cpp file has it's own unique integer i. Which I doubt is what you want.

Actually that use of static is deprecated (see The Good, the Bad, and the Deprecated). If that is what you want then the anonymous namespace should be used instead.

Enigma

Thanx! Enigma.
I will experimet with the "extern"
But the problem is that all the global variables and stuffs is defined multiple times and Dev-C++ doesn't like that.
Please comment my english!
Quote:Original post by Enigma
Quote:Original post by Cocalus

<good-stuff />

Another method is to use static int i. But in this case each cpp file has it's own unique integer i. Which I doubt is what you want.

Actually that use of static is deprecated (see The Good, the Bad, and the Deprecated). If that is what you want then the anonymous namespace should be used instead.

Enigma


I really need to sit down and read the standards book some day. Thanks for the correction.
Quote:Original post by masterviking
Thanx! Enigma.
I will experimet with the "extern"
But the problem is that all the global variables and stuffs is defined multiple times and Dev-C++ doesn't like that.


Yes; that's exactly what proper use of 'extern' is meant to solve. Your global variable definitions should go into the .cpp files; then the .h's provide 'extern' declarations

This topic is closed to new replies.

Advertisement