Thread: Multiple Definition problems

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    22

    Question Multiple Definition problems

    Hi guys,

    I wonder if you can help me out and point me to the area of my brain that is defective.

    Basically what I have is the following:

    Code:
    //main.h
    #ifndef _MAIN_H_
    #define _MAIN_H_
    
    #include "extra.h"
    
    #endif
    Code:
    //main.cpp
    #include "main.h"
    
    int main() {
        ext_implement = new Extra();
        return 0;
    }
    Code:
    //extra.h
    #ifndef _EXTRA_H_
    #define _EXTRA_H_
    
    class Extra {
        public:
            Extra();
            void DoSomething();
        private:
    };
    
    Extra *ext_implement;
    #endif
    Code:
    //extra.cpp
    #include "extra.h"
    
    Extra::Extra() {
    }
    
    void Extra::DoSomething() {
    }
    Code:
    //another.h
    #ifndef _ANOTHER_H_
    #define _ANOTHER_H_
    
    #include "extra.h"
    
    class Another {
        public:
            Another();
    };
    
    #endif
    Code:
    //another.cpp
    #include "another.h"
    
    Another::Another() {
        ext_implement->DoSomething();
    }
    And here is the errors I receive:

    obj\Debug\main.o: In function `main':
    C:/Personal/Code/Test/main.cpp:3: multiple definition of `ext_implement'
    obj\Debug\extra.o:C:/Personal/Code/Test/extra.cpp:3: first defined here
    obj\Debug\another.o: In function `ZN7AnotherC2Ev':
    C:/Personal/Code/Test/another.cpp:3: multiple definition of `ext_implement'
    obj\Debug\extra.o:C:/Personal/Code/Test/extra.cpp:3: first defined here



    I realise that I am defining ext_implement multiple times due to me having the line
    #include "extra.h" (where ext_implement is defined) in a few different places, but I am
    not sure of the right way to do it.

    I need to have access to that variable in a few different places, so I don't mind what the solution is really. As long as there is one

    If you need more detail please let me know.
    Any pointers would be greatly appreciated.

    Thanks in advance...
    Shiver

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The problem isn't the #include "extra.h" all over the place (although that might not be the best thing anyway). The problem is that you have a global variable that is defined multiple times. If you want ext_implement to be available to any file that includes extra.h, then you can declare it as extern in that file and define it in a single cpp file like extra.cpp.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Ok well a simple solution is to include "extra.h" before the others, and therefor the ext_implement is global then you can use it at any point in your program

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Raigne, I did try that but without any success.
    The errors it gives are at linking time and not compile, so I assume that the order of #includes doesn't make a difference? Correct me if I'm wrong...

    Daved, awesome-ness. That works perfectly
    Never used extern before. Are there any immediate down sides to doing it this way?
    I know that the multiple #includes of the same file all over the place looks bad, just too much of a beginner to know a better way to go about it

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's fine if you are using things from that file. In the code above, main.h doesn't use anything from extra.h, so it shouldn't include it. On the other hand, main.cpp does use something from there, so it should include extra.h directly. Same thing with another.h and another.cpp.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    shiver, my solution would have worked, but for it to work you have to get rid of all those includes everywhere. You should always try to only include 1 of each file.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Raigne, your "solution" only works in very limited cases, and is not a general solution. Specifically, it only works if it is possible to restructure code so that header files are only #include'd once within a complete project. It is not always possible to ensure that main.h or extra.h is only #include'd once, particularly in scenarios where multiple source files need to #include a particular header.

    Daved's solution (making the variable extern in the header file, and define it in exactly one source file) is a more general solution.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> You should always try to only include 1 of each file.
    You should always include each file as many times as you need it. If a header file declares a class, and you need that class in multiple source files, then include the header in multiple source files. Most projects with more than a few files will require this. In this case, all three source files require the inclusion of extra.h.

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    If you were forced to have only one #include of each of your .h files, there wouldn't even be a reason to make .h files; you could throw all the stuff at the top of the .CPP file.

    The very fact you're making a .h file at all should be a sign you plan to have it included in more than one source file.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    Thanks a lot guys for all the comments.
    I think I'll hang around these boards more often, so many intelligent people, perhaps I'll catch the disease

    Just to clarify, the .h files I'm using are pretty much only for defining classes, so I definately need them to be included in multiple places. That extern was exactly what I was looking for.

    Thanks again everyone.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    One other thing: identifiers starting with an underscore followed by another underscore or an uppercase letter are reserved by the implementation. You might want to use inclusion guards that are called HEADER_H_ or HEADER_H or some other variation.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    thanks dwks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with multiple pipes.
    By LightsOut06 in forum Linux Programming
    Replies: 3
    Last Post: 12-02-2010, 02:38 PM
  2. Error: Multiple definition of '_main'...
    By thunderdome in forum C Programming
    Replies: 14
    Last Post: 01-23-2006, 11:13 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Class definition Identifier problems
    By bladerunner627 in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2005, 06:03 AM
  5. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM