# multiple inclusion

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

## 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 on other sites
I think we would need to see some code to see what you are doing exactly.

##### Share on other sites
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 on other sites
Thanks very much.

Peter

##### 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 on other sites
Yes, i did that. Sorry for the confusion.

Peter