Error compiling multiple file projects:

G

Gaijinco

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

Gianni Mariani

Gaijinco said:
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.
 
H

Howard

Gaijinco said:
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
 
G

Gaijinco

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?
 
G

Gianni Mariani

Howard said:
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...
 
H

Howard

----- Original Message -----
From: "Gaijinco" <[email protected]>
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.)
 
H

Howard

----- Original Message -----
From: "Gianni Mariani" <[email protected]>
Newsgroups: comp.lang.c++
Sent: Friday, April 13, 2007 3:28 PM
Subject: Re: Error compiling multiple file projects:

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

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top