473,414 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

#ifndef #define #endif and multiple definitions problem

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
Jul 19 '05 #1
3 17926
> 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


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
Jul 19 '05 #2
Thanks Peter
You're absolutely right... I'm not thinking straight this
morning... Far too much coffee.

"Peter van Merkerk" <me*****@deadspam.com> skrev i en meddelelse news:bn************@ID-133164.news.uni-berlin.de...
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


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

Jul 19 '05 #3
prettysmurfed wrote:

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... :-)


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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: srinivas reddy | last post by:
I have defined some variables using #define preprocessor instruction. And then later I checked whether I had defined a variable. Sometimes even though a variable have been defined, #if...
5
by: DrUg13 | last post by:
#ifndef HEADER_H #define HEADER_H blah blal #endif So this tells the compiler That if its defined do not define it again. Could someone help me understand this. Does this mean that if I...
6
by: Peng Yu | last post by:
I want to define a macro #define FILEWR(FILENAME) which can be expanded to #ifndef OUTFILE #define OUTFILE ofstream out; #endif
25
by: John Hanley | last post by:
I have a program where both my main.c and program.c files use the program.h file. So I #include "program.h" in both the .c files. The program.h file has #ifndef PROGRAM_H #define PROGRAM_H...
9
by: Qiao Jian | last post by:
I am new to c. Today I just read an h file within which there is statements: #ifndef _RANDOM_H #define _RANDOM_H So what is the meaning or purpose of this statement? When should I use such...
3
by: iler.ml | last post by:
I am writing code that uses two third-party libraries. They both define same macro OP_ENCRYPT, and luckily for me, they both define it to 0. (In one include, it's '#define OP_ENCRYPT 0', in...
5
by: vfunc | last post by:
Despite a #ifndef I am getting a redefinition error The offending .h file looks like the following #ifndef DISTRIB_H #define DISTRIB_H double dblpi; // this is getting compiled twice ...
6
by: canoewhiteh2o | last post by:
I am converting a couple of C header files to C#. It is mainly just a bunch C structs but I am not sure how to handle the #ifdef and #ifndef in C#. For example: #ifndef DATE_TIME #define...
6
by: Johs | last post by:
Each time I make a new .h file in eclipse it starts with this: #ifndef FUNCS_H_ #define FUNCS_H_ // here goes all the code #endif /*FUNCS_H_*/
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.