Skip to main content
gamedev.net
🔒 Locked

Quick C++ question

Started by snisarenko May 2, 2004 at 4:59 AM 3 replies 750+ views
Original Post
snisarenko
snisarenko
When I create a .h file and I create global variables inside, I can't include that .h file into multiple .cpp file (only in one .cpp file). Why is that ? I am guessing that only one .cpp file can use those variables ? but I've seen quake source and it has many .h files with global variables that are included into multiple other .cpp files. P.S. I don't know, my head is just not working today [edited by - snisarenko on May 2, 2004 6:00:41 AM]
Ravyne
Ravyne
What you want to do is define the variable in the appropriate source file, then extern it in the header. Like so:

-- Source file --
int SomeStat = 10;
...

-- Header file --
extern int SomeStat;
...

Ravyne, NYN Interactive Entertainment
[My Site][My School][My Group]
throw table_exception("(? ???)? ? ???");
snisarenko
snisarenko
Oh yeah you are right quake source does use extern for all its global variables

So any variables defined in .h that are not marked extern can only be use by one .cpp file right ?

[edited by - snisarenko on May 2, 2004 6:15:30 AM]
Structural
Structural
quote:
Original post by snisarenko
Oh yeah you are right quake source does use extern for all its global variables

So any variables defined in .h that are not marked extern can only be use by one .cpp file right ?

[edited by - snisarenko on May 2, 2004 6:15:30 AM]


Indeed. This is because when you don''t, you try to create the same variable over and over again in each cpp file.
IMPORTANT, DO NOT FORGET TO DECLARE THE VARIABLE IN ONE CPP FILE!!

Extern means "it''s in some other, external, object file (intermediate file when compiling) and you''ll find it when linking". So, in ONE cpp file you need to declare the variable as not external: int myexternalvar;
If you don''t you get linker errors. ("unresolved external myexternalvar"or something along those lines)

STOP THE PLANET!! I WANT TO GET OFF!!
snisarenko
snisarenko
k thanx. It all makes sense now.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.