multiple inclusion

Started by
4 comments, last by petercnm 19 years, 1 month ago
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
Advertisement
I think we would need to see some code to see what you are doing exactly.
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Thanks very much.

Peter
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
Yes, i did that. Sorry for the confusion.
Thanks anyway for replying.

Peter

This topic is closed to new replies.

Advertisement