Using Multiple CPP files

E

ewpatton

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

Ian Collins

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

ewpatton

I've tried that with no such luck. The linker doesn't seem to realize
that they are one in the same...
 
S

Scott McPhillips [MVP]

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

raisenero

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

ewpatton

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

Greg Comeau

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

Greg Comeau

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.
 

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,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top