Thread: odd multiple declaration?

  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
    28,413
    Do not include source files, include header files instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,130
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,815
    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, 10: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, 06:39 AM