473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 2388
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
5
by: Mattias Thuresson | last post by:
Everytime I compile my project I'll get a compile error saying: C:\vss\DPC2\Banverket\Data\DPC2\DPCII Client\DPCApplication.cs(2): The type or namespace name 'ClientCore' does not exist in the...
3
by: Prem S | last post by:
Hi all I have a C#.NET solution that contains about 10 projects. One of the projects is a set of user built controls which gets referenced in a couple of projects. Some of the these projects,...
9
by: JTrigger | last post by:
When I compile my project using the IDE on a development machine it works just fine. When I compile it on the server using csc.exe, I get the following error when I try to bring it up in the web...
0
by: Herman Jones | last post by:
I'm getting the following error when I build a Class Library project: Embedding manifest... Project : error PRJ0002 : Error result 1 returned from 'C:\WINDOWS\system32\cmd.exe'. It happens with...
3
by: Phaitour | last post by:
Hi there, I'm working on developing a large Class Library project that is slowly becoming a shared "framework" library amongst multiple applications. As this shared library grows, I need to...
1
yabansu
by: yabansu | last post by:
Hi all, I implemented a basic client application. It communicates with server and works properly. Building processes succeed just by giving the following warning: LINK : D:\DOC\Visual Studio...
1
by: sahilrekhi | last post by:
Hi i am working on a graphical user interface deisgn programme. when i compile the file i i get the following output with the mentioned errors: 1>------ Rebuild All started: Project: guiq,...
2
by: yalbizu | last post by:
#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; const int NO_OF_STUDENTS=20; struct studentType { string studentFName; string studentLName;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.