Thread: Multiple definition error

  1. #1
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244

    Multiple definition error

    Hi everyone!

    I have a .h file in which I declare a global variable.
    Many .cpp files include my .h, and this generates an error in my linker.

    ////// my.h /////
    int variable;
    ////// 1.cpp //////
    #include ¨my.h¨
    ////// 2.cpp //////
    #include ¨my.h¨

    How can I solve this problem? Thanks any help!
    Nothing more to tell about me...
    Happy day =)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Only declarations should be in headers, but you can avoid multiple definition errors by enclosing the header in inclusion guards:
    Code:
    #ifndef MY_H
    #define MY_H
    
    int variable;
    
    #endif
    My best code is written with the delete key.

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    but even then the linker will complain if the include is used in more than one module.

    You may however declare the global in a cpp file and use the extern keyword in the .h file. For instance:

    // in the cpp module
    int variable;


    //in the .h file
    extern int variable;

    This will give every module that includes the .h file the ability to use it.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Prelude: sorry for not have told, but I already tryied to do this.
    FillYourBrain: it worked, I tryied to do this too, but in every .cpp I put extern, now, with the definition in my main .cpp file and just extern in .h worked.

    Thanks to both of you!!!!
    Nothing more to tell about me...
    Happy day =)

  5. #5
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    This also works
    Code:
    // main.cpp
    #define NEED_VARS
    #include "local.h"
    
    // local.h
    #ifdef NEED_VARS
         int var;
         float other var;
    #else
         extern int var;
         extern float var;
    #endif
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  6. #6
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    This is a bit strange dbgt goten, never saw nothing like this.
    Nothing more to tell about me...
    Happy day =)

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I don't suggest you do it that way.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Prelude: sorry for not have told, but I already tryied to do this.
    That's okay, it was a bogus answer written by a sleepy programmer.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM