Skip to main content
GameDev.net gamedev.net
🔒 Locked

directives

Started by Caesar Jul 29, 2001 at 4:06 AM 3 replies 300+ views
Original Post
Caesar
Caesar
Hi, I created a project and added a lot of header and source pairs. And then I''ve needed to include each header in each other header (so that I can use code from each file in each other file). But an problem occured, a "Already defined in xxx.obj" apeared. How can I solve this problem? All the header are locked (#ifndef #define #endif), the extern prefix is also useless (or at least in the way I use it). Any other notes to this topic also welcomed Many thanks
Caesar
Caesar
Yeah, but header files are used to declare this, I need to use, for example, variable X, declared in a.h (or whatever extension the file will have) and use it in another 10 files, I cannot imagine how to do it this way
Dactylos
Dactylos
declare a variable as extern in the headers and then define it in ONE .c/.cpp file for the variable to be useable across modules.

ex.

in foobar.h:
      
extern int foobar;


in foobar.cpp:
  
int foobar = 0xFF; // whatever value you want, or no value at all.


Just remember, that if you want the variable to have an initial value, only supply it in the DEFINITION (i.e. in the .c/.cpp file), never in the extern declaration (in the .h file).

Now you can include the .h file in as many modules as you want, and it shouldn't cause any problems.

Edited by - Dactylos on July 29, 2001 6:58:30 PM
Caesar
Caesar
As for variables, this fits, but what to do when it comes to classes, function etc?

sthing.h
  
class CSomething{
public:
int Hello();
};

extern CSomething something;


sthing.cpp
  
int CSomething::Hello()
{
return 0;
}


thanks


Edited by - Caesar on July 30, 2001 7:09:34 AM

Topic Locked

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

Sign in to reply to this topic.