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 3 17966
> 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.merke rk(at)dse.nl
Thanks Peter
You're absolutely right... I'm not thinking straight this
morning... Far too much coffee.
"Peter van Merkerk" <me*****@deadsp am.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.merke rk(at)dse.nl
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 !defined(var) construct
is returning true. I am using gcc3.0.1 on SunOS8. I would appreciate
if any body can tell me whether this is a bug or I am doing something
wrong.
tia,
Srinivas
|
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 use the same clasee in two different classes
|
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
|
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
....
#endif
Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.
|
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 statement in my own program?
Thank you so much!
| |
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 another include it's 0x0).
I cannot change contents of those two includes.
This generates comipler warning 'macro redefined'. It's easy
to suppress the warning with #ifdef. But I want to also
cross-check that those two macros have same value.
I am...
|
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
etc...
|
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 DATE_TIME unsigned long
#endif // NOT DATE_TIME
The #define DATE_TIME I am handling with:
public const UInt32 DATE_TIME;
|
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_*/
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |