473,395 Members | 1,679 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,395 software developers and data experts.

Using Multiple CPP files

I have a header that is shared among different CPP files for constants.
When Microsoft Visual C++ links the .obj files, it complains that these
names are all duplicates. How can I get it to realize that these are
coming from the same header and refer to the same things instead of
making two different copies?

Aug 18 '06 #1
8 4774
ew******@gmail.com wrote:
I have a header that is shared among different CPP files for constants.
When Microsoft Visual C++ links the .obj files, it complains that these
names are all duplicates. How can I get it to realize that these are
coming from the same header and refer to the same things instead of
making two different copies?
Have you declared the constants 'extern' in the common header?

--
Ian Collins.
Aug 18 '06 #2
I've tried that with no such luck. The linker doesn't seem to realize
that they are one in the same...

Ian Collins wrote:
ew******@gmail.com wrote:
I have a header that is shared among different CPP files for constants.
When Microsoft Visual C++ links the .obj files, it complains that these
names are all duplicates. How can I get it to realize that these are
coming from the same header and refer to the same things instead of
making two different copies?
Have you declared the constants 'extern' in the common header?

--
Ian Collins.
Aug 18 '06 #3
ew******@gmail.com wrote:
>>>I have a header that is shared among different CPP files for constants.
When Microsoft Visual C++ links the .obj files, it complains that these
names are all duplicates. How can I get it to realize that these are
coming from the same header and refer to the same things instead of
making two different copies?
Declare the variables, with extern, in the header file. Define the
variables in one and only one .cpp file.

extern int s; // .h

int s = 0; // .cpp

The first merely declares a symbol to the compiler. The second
allocates runtime storage, which must be done one place only for a
variaable.

--
Scott McPhillips [VC++ MVP]

Aug 18 '06 #4
ew******@gmail.com wrote:
I have a header that is shared among different CPP files for constants.
When Microsoft Visual C++ links the .obj files, it complains that these
names are all duplicates. How can I get it to realize that these are
coming from the same header and refer to the same things instead of
making two different copies?
I think you might be asking about a technique known as inclusion guard.
Like this:

#ifndef HEADERNAME_H
#define HEADERNAME_H

class foo {
int member;
};

#endif

A common practice is to replace HEADERNAME_H with the actual name of
the file. So if you named your file Students.h, you'd use STUDENTS_H.

Inclusion guard makes sure that your header is only included once.

Aug 18 '06 #5
That's the way I have done it... Apparently, the preprocessor
directives don't apply across the different CPP files when they are
compiled. So it compiles WinMain.cpp, and goes through the
preprocessor. When it's done it exits, then the next cpp file is
compiled, and it goes through it all again.

I thought that was how it was supposed to work originally, but I guess
I was mistaken.

raisenero wrote:
ew******@gmail.com wrote:
I have a header that is shared among different CPP files for constants.
When Microsoft Visual C++ links the .obj files, it complains that these
names are all duplicates. How can I get it to realize that these are
coming from the same header and refer to the same things instead of
making two different copies?

I think you might be asking about a technique known as inclusion guard.
Like this:

#ifndef HEADERNAME_H
#define HEADERNAME_H

class foo {
int member;
};

#endif

A common practice is to replace HEADERNAME_H with the actual name of
the file. So if you named your file Students.h, you'd use STUDENTS_H.

Inclusion guard makes sure that your header is only included once.
Aug 18 '06 #6
Thanks Scott! That seems to have solved the problem.

Scott McPhillips [MVP] wrote:
ew******@gmail.com wrote:
>>I have a header that is shared among different CPP files for constants.
When Microsoft Visual C++ links the .obj files, it complains that these
names are all duplicates. How can I get it to realize that these are
coming from the same header and refer to the same things instead of
making two different copies?

Declare the variables, with extern, in the header file. Define the
variables in one and only one .cpp file.

extern int s; // .h

int s = 0; // .cpp

The first merely declares a symbol to the compiler. The second
allocates runtime storage, which must be done one place only for a
variaable.

--
Scott McPhillips [VC++ MVP]
Aug 18 '06 #7
In article <11*********************@b28g2000cwb.googlegroups. com>,
<ew******@gmail.comwrote:
>I have a header that is shared among different CPP files for constants.
When Microsoft Visual C++ links the .obj files, it complains that these
names are all duplicates. How can I get it to realize that these are
coming from the same header and refer to the same things instead of
making two different copies?
It could be a few things, but sounds like you need extern everywhere
(in the header) except for one place (in one non-header file).
I would suggest getting Stroustrup's The C++ Programming Language
and checking out his Chapter 9. If you don't have this text, get it.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 18 '06 #8
In article <11********************@i42g2000cwa.googlegroups.c om>,
<ew******@gmail.comwrote:
>That's the way I have done it... Apparently, the preprocessor
directives don't apply across the different CPP files when they are
compiled. So it compiles WinMain.cpp, and goes through the
preprocessor. When it's done it exits, then the next cpp file is
compiled, and it goes through it all again.
If you have X should files, then you should be able to reduce it to
a test case with only 2. And with a few lines in each file.
Do that and post those ~20 lines in your 3 files (2 source, 1 header) here.
And also the error you are getting.
--
Greg Comeau / 20 years of Comeauity! Intel Mac Port now in alpha!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Aug 18 '06 #9

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

Similar topics

0
by: Dominik | last post by:
I am writing application which must download multiple files simultaneous. Initially I wanted to use multiple threads to achieve this task but later I heard that Net is "using" separate thread from...
6
by: x. zhang | last post by:
Hi Guys, We know that we can use <input type=file ...> to upload one file per time to the server. My question is if there are some way to upload multiple files per time to the server. (Of...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
3
by: codabill | last post by:
Can someone please refer me to books, articles, websites where I can obtain information on how to set up VS2003 for team development. I am a project manager for a non-trivial military software...
1
by: PK9 | last post by:
I'm building a windows app using C#. The goal is to merge portions of multiple xml files into one. I currently have an .xsl stylesheet that pulls in the required sections of multiple xml files...
2
by: Sam | last post by:
Hi All, I have a solution which consists of multiple projects and each of these projects has their own app.config file. The problem is that all of my projects in the solution pull keys from the...
0
by: pbd22 | last post by:
Hi. I am having a really tough time here and would appreciate some help. i am using an iFrame to pass a url string of files to upload to server. on the client i have created a multiple...
7
by: Pete | last post by:
I need to import multiple tab delimited files for which I do not have the formats. All files have column headers in the the line. The files must be dynamic. The only common value is that the...
43
by: bonneylake | last post by:
Hey Everyone, Well this is my first time asking a question on here so please forgive me if i post my question in the wrong section. What i am trying to do is upload multiple files like gmail...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.