Connecting Tech Pros Worldwide Forums | Help | Site Map

#ifndef #define #endif and multiple definitions problem

prettysmurfed
Guest
 
Posts: n/a
#1: Jul 19 '05
Hi all

I have this, probably stupid question, how to avoid multiple
definitions when a header file is included more than once.
I thought, when you wrote the header-file and used the
ifndef/define directives, the code between the statements
define and endif was only compiled once. But this seems
to be incorrect, as I get a whole bunch of errors when
I compile the critter.
For example if I include header.h more than once the
variable foo will cause a multiple definition compile error...

#ifndef HEADER_H_
#define HEADER_H_

int foo;

#endif

When you stop laughing at my question, I would really
appreciate a solution... :-)

Go steady
Martin



Peter van Merkerk
Guest
 
Posts: n/a
#2: Jul 19 '05

re: #ifndef #define #endif and multiple definitions problem


> I have this, probably stupid question, how to avoid multiple[color=blue]
> definitions when a header file is included more than once.
> I thought, when you wrote the header-file and used the
> ifndef/define directives, the code between the statements
> define and endif was only compiled once. But this seems
> to be incorrect, as I get a whole bunch of errors when
> I compile the critter.
> For example if I include header.h more than once the
> variable foo will cause a multiple definition compile error...
>
> #ifndef HEADER_H_
> #define HEADER_H_
>
> int foo;
>
> #endif[/color]

Replace "int foo;" with "extern int foo;" in the header file, one
translation unit (i.e. .cpp or .cc file) should have the "int foo;"
line.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


prettysmurfed
Guest
 
Posts: n/a
#3: Jul 19 '05

re: #ifndef #define #endif and multiple definitions problem


Thanks Peter
You're absolutely right... I'm not thinking straight this
morning... Far too much coffee.

"Peter van Merkerk" <merkerk@deadspam.com> skrev i en meddelelse news:bnam9u$v6189$1@ID-133164.news.uni-berlin.de...[color=blue][color=green]
> > I have this, probably stupid question, how to avoid multiple
> > definitions when a header file is included more than once.
> > I thought, when you wrote the header-file and used the
> > ifndef/define directives, the code between the statements
> > define and endif was only compiled once. But this seems
> > to be incorrect, as I get a whole bunch of errors when
> > I compile the critter.
> > For example if I include header.h more than once the
> > variable foo will cause a multiple definition compile error...
> >
> > #ifndef HEADER_H_
> > #define HEADER_H_
> >
> > int foo;
> >
> > #endif[/color]
>
> Replace "int foo;" with "extern int foo;" in the header file, one
> translation unit (i.e. .cpp or .cc file) should have the "int foo;"
> line.
>
> --
> Peter van Merkerk
> peter.van.merkerk(at)dse.nl
>
>[/color]


MPBroida
Guest
 
Posts: n/a
#4: Jul 19 '05

re: #ifndef #define #endif and multiple definitions problem


prettysmurfed wrote:[color=blue]
>
> Hi all
>
> I have this, probably stupid question, how to avoid multiple
> definitions when a header file is included more than once.
> I thought, when you wrote the header-file and used the
> ifndef/define directives, the code between the statements
> define and endif was only compiled once. But this seems
> to be incorrect, as I get a whole bunch of errors when
> I compile the critter.
> For example if I include header.h more than once the
> variable foo will cause a multiple definition compile error...
>
> #ifndef HEADER_H_
> #define HEADER_H_
>
> int foo;
>
> #endif
>
> When you stop laughing at my question, I would really
> appreciate a solution... :-)[/color]

The #ifndef/#define/#endif only prevents "int foo;"
from being compiled in one file on EACH compilation.

If you compile x.cpp separately from y.cpp, then
EACH of them will end up defining "foo". And the
linker then sees multiple definitions.

The "extern" trick is the way to avoid the problem
entirely.

Mike
Closed Thread