Jump to content
  • Advertisement
Sign in to follow this  
petercnm

multiple inclusion

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, i have this problem with my header files. I have to use the same header file in two .cpp files. I've protected my .h file with #ifndef #define . . #endif, but i still get "multiple definition of...", so i'm resorting to the use of 'extern', but would like to find out what my problem is. I'm using Dev-Cpp, maybe some compiler option ? Thanks in advance. Peter

Share this post


Link to post
Share on other sites
Advertisement
Inclusion guards keep the same file from accidently including itself, which would be bad.

They don't protect you from having multiple copies of a variable. If you declare a variable in a header, every file that includes it is going to get a copy of it. When link time comes around it's going to go 'Oh my god! There's 30 dozen variables all named foo! Which one do I use? What do I do? Ahhh!', promptly followed by it running screaming off the edge of a cliff.

And that's where 'extern' comes in, which you've already discovered.

Share this post


Link to post
Share on other sites
Ok, in your example you said you use #ifndef #define etc, but you didn't put any names after them. Just incase thats how your useing them read on: if you have the qualifier bit then ignore me [grin]

You need to actually define something for this to work. #define BLAH defines whatever BLAH is to the precompiler, and later you can check if it is defined. Thats how the header guard thing works. First time the file is included it says "if BLAH is not defined then continue", and it goes on. Then the first thing you do is define BLAH. Next time you try to include the file it will say "if blah is not defined ... oh wait it is don't bother" and skip down to the #endif tag. So you need to put something after the #define call - the thing you'll be defining as shown below:


#ifndef __FILE1_H__
#define __FILE1_H__

// stuff herre

#endif




EDIT: too slow

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!