Sign in to follow this  
masterviking

Weird header linking in c++

Recommended Posts

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 [i mean the .h files] :) ) 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this