Using Multiple CPP files

Discussion in 'C++' started by ewpatton@gmail.com, Aug 18, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Ian Collins Guest

    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.
     
    Ian Collins, Aug 18, 2006
    #2
    1. Advertising

  3. Guest

    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:
    > 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, 2006
    #3
  4. 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]
     
    Scott McPhillips [MVP], Aug 18, 2006
    #4
  5. raisenero Guest

    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.
     
    raisenero, Aug 18, 2006
    #5
  6. Guest

    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:
    > 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, 2006
    #6
  7. Guest

    Thanks Scott! That seems to have solved the problem.

    Scott McPhillips [MVP] wrote:
    > 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, 2006
    #7
  8. Greg Comeau Guest

    In article <>,
    <> 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?


    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?
     
    Greg Comeau, Aug 18, 2006
    #8
  9. Greg Comeau Guest

    In article <>,
    <> wrote:
    >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?
     
    Greg Comeau, Aug 18, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DrUg13
    Replies:
    1
    Views:
    486
    DrUg13
    Feb 10, 2004
  2. Alex Vinokur
    Replies:
    7
    Views:
    416
    Greg Comeau
    Nov 15, 2004
  3. Vinu
    Replies:
    9
    Views:
    642
  4. Replies:
    4
    Views:
    974
    M.E.Farmer
    Feb 13, 2005
  5. www.hitechskill.com
    Replies:
    0
    Views:
    1,368
    www.hitechskill.com
    Apr 9, 2006
Loading...

Share This Page