473,761 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ahww my make file, redefinition despite #ifndef

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...
#endif

.... and my make file is as follows
CC = g++
# CFLAGS = -ggdb
INC =
SRCS = loader.cpp distrib.cpp main.cpp
OBJS = $(SRCS:.c=.o)
LIBS =
TARGET = ldisp.exe

$(TARGET): $(OBJS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LIBS)

# clean:
# rm -f $(OBJS) $(TARGET)

Aug 16 '06 #1
5 3661

<vf***@talktalk .netwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
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...
#endif

... and my make file is as follows
(Makefiles are off-topic here.)

The problem is that you're defining global variables in a header file.
Don't do that. It's better to not use global variables at all. But if you
have to, and if multiple source files need access to them, then define them
in an implementation file (a .cpp file), and in the header file put the
keyword "extern" before the above definition. That turns them into a
external declarations, allowing other unit to see them, and preventing
multiple definitions.

-Howard


Aug 16 '06 #2
vf***@talktalk. net <vf***@talktalk .netwrote:
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
If you want all translation units to use the same variable, try doing

extern double dblpi;

in your header, and then define it in exactly one .cpp file.

The #ifndef/#endif inclusion guards only protect from the same header
being included multiple times in a single translation unit.
etc...
#endif
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Aug 16 '06 #3

Marcus Kwok wrote:
>
If you want all translation units to use the same variable, try doing

extern double dblpi;

in your header, and then define it in exactly one .cpp file.
I did this now and I still get the error so the problem must be with my
make file. Where can I ask about that ?

Aug 16 '06 #4

<vf***@talktalk .netwrote in message
news:11******** **************@ i3g2000cwc.goog legroups.com...
>
Marcus Kwok wrote:
>>
If you want all translation units to use the same variable, try doing

extern double dblpi;

in your header, and then define it in exactly one .cpp file.

I did this now and I still get the error so the problem must be with my
make file. Where can I ask about that ?
I seriously doubt it's related to your makefile. What error are you getting
(exactly), and what does the code look like? We need to see both the extern
declaration you say you've done, and the actual definition. There should be
only one definition, and that should be in a file that is not #include'd by
any other file. You will not get a multiple definition error from a
variable declared as extern, unless there are multiple definitions for it
somewhere else.

-Howard
Aug 16 '06 #5
I seriously doubt it's related to your makefile. What error are you getting
(exactly), and what does the code look like? We need to see both the extern
declaration you say you've done, and the actual definition. There should be
only one definition, and that should be in a file that is not #include'd by
any other file. You will not get a multiple definition error from a
variable declared as extern, unless there are multiple definitions for it
somewhere else.

-Howard
Sorry, my mistake I moved the declaration to the cpp file and it
worked.

Thanks.

Aug 16 '06 #6

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

Similar topics

7
28953
by: Karen | last post by:
Hi, I have one constant variable and want to use it in two files. I put it in the header file and then include the header file. The compiler always say "error C2370: 'arraysize' : redefinition; different storage class". What shall I do? Files are listed below. I'm using Visual C++ 6.0 By the way, how to use STL (vector , for example) to create a multi-dimension array?
6
2307
by: Shen | last post by:
I saw this conclusion in a couple of groups. For example, if you define a class template, in VC++ you have to merge the .h and .cpp files into one file and include it in other files where they have something to do with this template class. Otherwise you'll get Link errors(unresolved external symbol...LNK2001/LNK2019). Is it still true now? Has MS Visual studio 2003 done something for it? or does anyone figure out a solution other than...
5
2168
by: Nomak | last post by:
Hello all, i have two template classes which needs each other. I tried to write some fwd decl / decl / impl in a good way but i can't get it to compile. Explanations: - .hh files are for declaration (template or not) - .hxx files are template impl
7
1858
by: Stefan | last post by:
Hi, I am trying to get my structure to be externed so I can use it in all my projects source files, but the code is generating errors. Here is some of my code: "Globals.cpp" typedef struct SPECKEY { bool BeenUsed;
7
12967
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file needs to access the class defined in my second file and I can't figure out how to work this right. if I use an #include in my third file, my Main gives me a compile-time class redefinition error. if I don't, the third file can't "see" the second
3
7276
by: JCB | last post by:
Hi, I have two C file which are referencing in a circular way. To simplify I use an example with car : Suppose I have a program for cars. I have car.c, structure car and functions prototypes of car.c are in a file car.h
13
3587
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the amount of preprocessor directives used in these code. Can anyone please tell me how to actually use these preprocessor directives in a more professional way like selecting particular lines of code suited for some particular hardware etc...
18
3586
by: Al | last post by:
I'm still trying to do this but it never worked! In a .cpp file, I write the code, and at the beginning, I write: #ifndef MYLIST_H #define MYLIST_H ....to end: #endif What's wrong with it for creating a header file when compiling? Do I need to write in the block the #include (s)?
6
50211
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
9538
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, 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...
0
9353
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,...
0
10123
Oralloy
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...
1
9909
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
tracyyun
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...
0
8794
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7342
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 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...
0
5241
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...
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.