Thread: Multiple definition linker error

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    596

    Multiple definition linker error

    Why does code like this
    Code:
    #ifndef _foo_h
    #define _foo_h
    
    class Foo
    {
      public:
      static int sInt;
    };
    
    int Foo::sInt = 1;
    
    #endif
    cause linker error "multiple definition of 'Foo::sInt' when foo.h is included in several source files? I was under the impression that the purpose of header guards was precisely to prevent such an error.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Nope, you need to put the line:
    Code:
    int Foo::sInt = 1;
    in a .cpp file.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by bithub View Post
    Nope, you need to put the line:
    Code:
    int Foo::sInt = 1;
    in a .cpp file.
    Yes, I already figured that out. My question is why? (If the guards prevent multiple definition of Foo, why don't they prevent multiple definition of Foo::sInt?)

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by R.Stiltskin View Post
    Yes, I already figured that out. My question is why? (If the guards prevent multiple definition of Foo, why don't they prevent multiple definition of Foo::sInt?)
    It prevents multiple inclusion in any one source module. It doesn't prevent anything at all across different modules.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by brewbuck View Post
    It prevents multiple inclusion in any one source module. It doesn't prevent anything at all across different modules.
    Good to know. Thanks.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    It prevents multiple inclusion in any one source module. It doesn't prevent anything at all across different modules.
    Damn really? That's crazy

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by brewbuck View Post
    It prevents multiple inclusion in any one source module. It doesn't prevent anything at all across different modules.
    Wait a minute. How can that be? Of course I can have
    Code:
    #ifndef _foo_h
    #define _foo_h
    
    class Foo
    {
      public:
      int bar;
    };
    
    #endif
    and #include "foo.h" in several different source files with no problem. It's only the initialization of the static member that causes a problem. Or does "different modules" mean something else?

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by R.Stiltskin View Post
    Wait a minute. How can that be? Of course I can have
    Code:
    #ifndef _foo_h
    #define _foo_h
    
    class Foo
    {
      public:
      int bar;
    };
    
    #endif
    and #include "foo.h" in several different source files with no problem. It's only the initialization of the static member that causes a problem. Or does "different modules" mean something else?
    Uh... Yeah, because in this case you're declaring something, not creating an object/variable...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    You can think of the headers as basically being copy/pasted into the source files where they are included. So if you put global declarations inside a header, the linker will find them in each module which had it included. This is why you separate .h and .c files so the .c is compiled once, and any other modules which would need something from it include the header files to get references. I'm not sure about C++ static vars, but you might be able to declare this in the .cpp file and then declare it extern in the .h file. Anyone know this off hand? I'd experiment but have to go.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by brewbuck View Post
    Uh... Yeah, because in this case you're declaring something, not creating an object/variable...
    [smacks self in head...] Yeah, I lost sight of the declaration/definition distinction.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by neandrake View Post
    I'm not sure about C++ static vars, but you might be able to declare this in the .cpp file and then declare it extern in the .h file. Anyone know this off hand? I'd experiment but have to go.
    It's the same as any global var. Define in one source file, declare extern in a header.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM