[RESOLVED] Crazy Multiple Definition Problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: [RESOLVED] Crazy Multiple Definition Problem

  1. #1
    Join Date
    Sep 2010
    Location
    Middletown, Connecticut
    Posts
    3

    [RESOLVED] Crazy Multiple Definition Problem

    Theres quite a bit of code, so I don't know if posting it will help or just make the page full of useless info, so I'll just state the problem and info.

    I declare a std::map called KeyMap, and a pointer to a class called Proc_Button, and receive multiple definition errors.

    Which is usually easy enough to fix. However, I have commented out every definition for those variables, and the problem persists. Commenting out the declaration, definitions, and uses of those variables allow me to compile with no problem. On top of there being no definitions involved in the 'multiple definition' error, GCC's error log points me to headers and source files where those particular variables aren't used or mentioned, even in commented out code. One line even brings me to stl_tree.h

    I have tried commenting the relevant lines out, I have created a fresh Code::Blocks project, changed the name and location of where those variables are declared, and spent about 20 minutes or so Googling around for a solution, so I'm completely stumped and have no idea how to fix this rather annoying problem.

    Extra Info:
    Both KeyMap and Proc_Button are in a namespace
    Any new, similar declarations get the same problems
    KeyMap was working, it just stopped upon the latest attempts to compile.
    Proc_Button is declared just like a variable with a similar use called Proc_Input which is working fine
    IDE is Code::Blocks 10.5
    Compiler is MingW's GNU GCC Compiler

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Crazy Multiple Definition Problem

    If the variables aren't even declared anywhere, then any attempts to use them should result in a compile error before you even get to the linker.

    I suspect what you may be facing is a modification date issue; some object file isn't being recompiled when it should be. Creating a new project *should* have bypassed that, but I guess it's possible that's still the problem. Do a make clean, or a rebuild all.

  3. #3
    Join Date
    Sep 2010
    Location
    Middletown, Connecticut
    Posts
    3

    Re: Crazy Multiple Definition Problem

    It compiles fine when it's not declared. The problem arises with a declaration. I've been using rebuild all the entire time I spent on this, so that hasn't helped. I just tried cleaning the project and the errors are still popping up when I rebuild.

    Thanks for the advice though.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Crazy Multiple Definition Problem

    Make sure any declaration of the variable in a header file uses the extern keyword, and that only one source file contains the actual definition.

  5. #5
    Join Date
    Sep 2010
    Location
    Middletown, Connecticut
    Posts
    3

    Re: Crazy Multiple Definition Problem

    That solved the problem, never had to use extern before so the thought didn't pop into my head. Thanks very much for helping with my stupid mistake.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Crazy Multiple Definition Problem

    Another option to consider, perhaps more "C++ish", would be a variation on the singleton pattern:

    Some h file
    Code:
    struct Globals
    {
        static int& getMyIntGlobal()
        {
            static int var;
            return var;
        }
        static std::string& getMyStringGlobal()
        {
            static std::string var;
            return var;
        }
    };
    // some cpp file
    Code:
    int main()
    {
        Globals::getMyIntGlobal() = 5;
        cout << Globals::getMyStringGlobal();
    }
    Of course, this requires more design effort up-front.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center