Multiple Definition Compile Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mikejfe
    New Member
    • Feb 2007
    • 12

    Multiple Definition Compile Error

    Hi all. I am having multiple definition compiling errors. I included the #ifndef, #define, #endif lines in my header files. I've seen a post on here suggesting using extern. That didn't work for me either. This is a snippet from my header file.

    #ifndef ListVars_h
    #define ListVars_h

    extern int bonesDL = 3;
    extern int marrowDL = 4;

    #endif

    If I include ListVars.h into multiple .cpp files and compile, I get the error. Any ideas? I'm using Dev-C++ with Windows XP.

    Thank you!
    -Michael
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You can not initialise in a declaration so you need

    Code:
    extern int bonesDL;
    extern int marrowDL;
    in your header and

    Code:
    int bonesDL = 3;
    int marrowDL = 4;
    in a source file somewhere

    Comment

    • mikejfe
      New Member
      • Feb 2007
      • 12

      #3
      Hmm. Well I tried a few different things and I still get the multiple definition error. Maybe I didn't quite understand your response (sorry). The header file now reads:
      Code:
      #ifndef ListVars_h
      #define ListVars_h
      
      extern int bonesDL;
      
      #endif
      One thing I tried was adding a new function:
      Code:
      #include "ListVars.h"
      
      void initVars()
      {
      int bonesDL=3;
      }
      And then I tried:
      Code:
      #include "ListVars.h"
      
      void initVars()
      {
      bonesDL=3;
      }
      And finally I tried (as a last effort):
      Code:
      #include "ListVars.h"
      
      int bonesDL=3;
      void initVars()
      {
      }
      I also tried all of these variations without using extern in the header file.

      Thanks for your help,
      -Michael

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        the declaration in your header files looks OK:
        Code:
        #ifndef ListVars_h
        #define ListVars_h
        
        extern int bonesDL;
        
        #endif
        it must be defined in only ONE of your .c files
        Code:
        int bonesDL=3;
        if you are getting multiple definitions of this identifier you must have it defined in more than one c file

        in this version
        Code:
        #include "ListVars.h"
        
        void initVars()
        {
        int bonesDL=3;
        }
        you define it as a local variable, remove the 'int'

        Comment

        • mikejfe
          New Member
          • Feb 2007
          • 12

          #5
          It finally compiled! I'm pretty new to writing C++ programs that are more advanced than "Hello World." My grad professor stuck me on a program that requires knowledge of openGL and C++ ... and I've been struggling. Thank you so much for your help!

          Comment

          Working...