469,632 Members | 1,639 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

Error compiling multiple file projects:

I'm having a weird error compiling a multiple file project:

I have three files:

tortuga.h where I have declared 5 global variables and prototypes for
some functions.
tortuga.cpp where I implement all of the functions of tortuga.h
main.cpp where I use the functions implemented in tortuga.cpp

I create the objetc file without problem. But when I try to compile
"main.cpp" it says:

"multiple declaration of..." for each one of the global variables.

What am I doing wrong?

Thanks.

Apr 13 '07 #1
6 2170
Gaijinco wrote:
I'm having a weird error compiling a multiple file project:

I have three files:

tortuga.h where I have declared 5 global variables and prototypes for
some functions.
tortuga.cpp where I implement all of the functions of tortuga.h
main.cpp where I use the functions implemented in tortuga.cpp

I create the objetc file without problem. But when I try to compile
"main.cpp" it says:

"multiple declaration of..." for each one of the global variables.

What am I doing wrong?
Run just the pre-processor over main.cpp and see where they are multiply
defined. It appears that you're including the header twice somehow.
Apr 13 '07 #2

"Gaijinco" <ga******@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
I'm having a weird error compiling a multiple file project:

I have three files:

tortuga.h where I have declared 5 global variables and prototypes for
some functions.
tortuga.cpp where I implement all of the functions of tortuga.h
main.cpp where I use the functions implemented in tortuga.cpp

I create the objetc file without problem. But when I try to compile
"main.cpp" it says:

"multiple declaration of..." for each one of the global variables.

What am I doing wrong?
Don't declare global variables in header files. Instead, if you want them
to be visible from multiple source files, then move the declarations to a
source file, and then declare them as "extern" in the header file.

Better yet, don't use global variables at all. But that's another
question...

-Howard

Apr 13 '07 #3
It appears that you're including the header twice somehow.

Should be enough to use

#ifndef FILE_H
#define FILE_H

#endif

to stop including the header more than once?

Apr 13 '07 #4
Howard wrote:
>
"Gaijinco" <ga******@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
>I'm having a weird error compiling a multiple file project:

I have three files:

tortuga.h where I have declared 5 global variables and prototypes for
some functions.
tortuga.cpp where I implement all of the functions of tortuga.h
main.cpp where I use the functions implemented in tortuga.cpp

I create the objetc file without problem. But when I try to compile
"main.cpp" it says:

"multiple declaration of..." for each one of the global variables.

What am I doing wrong?


Don't declare global variables in header files. Instead, if you want
them to be visible from multiple source files, then move the
declarations to a source file, and then declare them as "extern" in the
header file.
What is the difference between "declare" and "define" ?
>
Better yet, don't use global variables at all. But that's another
question...
Globals are a very important part of the language. You can't do without
them "at all". However, they are often misused but that is still no
reason to outlaw them.

Reasons globals are good.

a) Constants - objects that do not change throughout the life of the
program (at least after main()) is called.

b) Registries - objects that are a list of items for that executable
(not specific to an instance of an object). e.g. Factories, plugins,
command line specifiers, initializer specifiers, etc...
Apr 13 '07 #5

----- Original Message -----
From: "Gaijinco" <ga******@gmail.com>
Newsgroups: comp.lang.c++
Sent: Friday, April 13, 2007 3:24 PM
Subject: Re: Error compiling multiple file projects:

It appears that you're including the header twice somehow.

Should be enough to use

#ifndef FILE_H
#define FILE_H

#endif

to stop including the header more than once?
That will stop errors relating to multiple type definitions, but that's not
his problem. He's getting "multiple declaration" errors on global variables
defined in a header. If he fixes the error of the multiple declarations by
using include guards, then what will happen is that he'll get "variable
undefined" errors from the second and subsequent files attempting to use
those variables. The way to allow multiple compilation units to see a
global variable definition is to use the "extern" keyword in a header file,
and move the declaration/definition into an implementation file.

-Howard

(P.S., sorry for replying directly to you first; I'm still getting used to
this new newsreader.)

Apr 16 '07 #6

----- Original Message -----
From: "Gianni Mariani" <gi*******@mariani.ws>
Newsgroups: comp.lang.c++
Sent: Friday, April 13, 2007 3:28 PM
Subject: Re: Error compiling multiple file projects:

Howard wrote:
>>
"Gaijinco" <ga******@gmail.comwrote in message
news:11**********************@p77g2000hsh.googleg roups.com...
>>I'm having a weird error compiling a multiple file project:

I have three files:

tortuga.h where I have declared 5 global variables and prototypes for
some functions.
tortuga.cpp where I implement all of the functions of tortuga.h
main.cpp where I use the functions implemented in tortuga.cpp

I create the objetc file without problem. But when I try to compile
"main.cpp" it says:

"multiple declaration of..." for each one of the global variables.

What am I doing wrong?


Don't declare global variables in header files. Instead, if you want
them to be visible from multiple source files, then move the declarations
to a source file, and then declare them as "extern" in the header file.

What is the difference between "declare" and "define" ?
Are you asking, or pointing out a mistake in my usage? I'm never quite sure
how to refer to asomething like this:

const int count = 36;

It's both a declaration AND a definition, right?
>
>>
Better yet, don't use global variables at all. But that's another
question...

Globals are a very important part of the language. You can't do without
them "at all". However, they are often misused but that is still no
reason to outlaw them.

Reasons globals are good.

a) Constants - objects that do not change throughout the life of the
program (at least after main()) is called.

b) Registries - objects that are a list of items for that executable (not
specific to an instance of an object). e.g. Factories, plugins, command
line specifiers, initializer specifiers, etc...
Agreed. I probably should have said "avoid them" instead of "don't use
[them] at all". But when someone talks about declaring a bunch of "global
variables", it's most likely not a good design.

-Howard

(P.S., sorry for replying directly to you first; I'm still getting used to
this new newsreader.)

Apr 16 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Peter Frost | last post: by
5 posts views Thread by Mattias Thuresson | last post: by
3 posts views Thread by Prem S | last post: by
9 posts views Thread by JTrigger | last post: by
reply views Thread by Herman Jones | last post: by
1 post views Thread by sahilrekhi | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.