odd multiple declaration?

This is a discussion on odd multiple declaration? within the C++ Programming forums, part of the General Programming Boards category; I have three files: TestA.cpp: Code: #include "CmnFunc.cpp" and TestB.cpp Code: #include "CmnFunc.cpp" and CmnFunc.cpp: Code: #ifndef CMNFNC #define CMNFNC ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    5

    odd multiple declaration?

    I have three files:

    TestA.cpp:

    Code:
    #include "CmnFunc.cpp"
    and TestB.cpp

    Code:
    #include "CmnFunc.cpp"
    and CmnFunc.cpp:

    Code:
    #ifndef CMNFNC
    #define CMNFNC
    
    int randInt(int start, int end){
    
    ...
    
    
    }
    
    #endif
    Compiing with 'g++ TestA.cpp TestB.cpp' gives a multiple definition error. I thought the #ifndef bit would protect against this? Any ideas?

    Thanks very much

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,181
    Do not include source files, include header files instead.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    5
    I know but shouldn't the #ifndef protect anyway??

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    Those inclusion guards only work per file. If you have two source files, you get multiple definitions. One per file
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    5
    makes sense i guess, thanks!

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    If you insist on including implementations in other files, you should at the very least declare them "inline" and use a .h extension, not a .cpp extension.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    5
    I'm not using the code, just asking for an explanation of the behavior thanks

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,534
    The explanation is that;

    1) TestA.cpp is compiled to a TestA.o which has a single definition of your randInt() function.

    2) TestB.cpp is compiled to a TestB.o which has a single definition of your randInt() function.

    3) The include guards in CmnFunc.cpp mean that, if TestA.cpp or TestB.cpp was to #include CmnFunc.cpp more than once, then statements 1 and 2 are still true.

    4) The linker, when it attempts to build your executable from TestA.o and TestB.o (and any other object or library files it links in) encounters a definition of randInt() in both TestA.o and TestB.o. In other words, it encounters two definitions. Hence it complains about a multiple definition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 11:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21