468,512 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 4436
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

121 posts views Thread by typingcat | last post: by
3 posts views Thread by Random Person | last post: by
3 posts views Thread by codabill | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.