ahww my make file, redefinition despite #ifndef

V

vfunc

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)
 
H

Howard

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
 
M

Marcus Kwok

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.
 
V

vfunc

Marcus said:
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 ?
 
H

Howard

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
 
V

vfunc

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top