Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Visual Studio error LNK2005: variable XXX redefined in xxx.obj

Visual Studio error LNK2005: variable XXX redefined in xxx.obj

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpc++visual-studiotutorial
16 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Javier Luis Lopez
    wrote on last edited by
    #1

    I tried to isolate the problem It appears when a global variable is declared at the .h header file and the header file is called from 2 or more cpp files, as example I tried to link the following 3 files:

    //==File: header.h
    #pragma once
    #include int variable;
    //==end header.h

    //==File: main.cpp
    #include "header.h"
    void main()
    {
    variable=0;
    }
    //==end main.h

    //File: source2.cpp
    #include "header.h"

    void change()
    {
    variable=0;
    }
    //==end source2.cpp

    Then the following LINKER error appear: 1>source2.obj : error LNK2005: redefined "int variable" (?variable@@3HA) in main.obj The only way I could "fix" the problem is in project properties> linker>command line add: /FORCE:MULTIPLE Unfortunately it can hide possible problems I think the problem is that the #pragma once does not work. It does not work also to define a constant and use #ifndef to avoid reading 2 times.

    L D _ 3 Replies Last reply
    0
    • J Javier Luis Lopez

      I tried to isolate the problem It appears when a global variable is declared at the .h header file and the header file is called from 2 or more cpp files, as example I tried to link the following 3 files:

      //==File: header.h
      #pragma once
      #include int variable;
      //==end header.h

      //==File: main.cpp
      #include "header.h"
      void main()
      {
      variable=0;
      }
      //==end main.h

      //File: source2.cpp
      #include "header.h"

      void change()
      {
      variable=0;
      }
      //==end source2.cpp

      Then the following LINKER error appear: 1>source2.obj : error LNK2005: redefined "int variable" (?variable@@3HA) in main.obj The only way I could "fix" the problem is in project properties> linker>command line add: /FORCE:MULTIPLE Unfortunately it can hide possible problems I think the problem is that the #pragma once does not work. It does not work also to define a constant and use #ifndef to avoid reading 2 times.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You should declare it extern in the header, and define it in only one of your .cpp files. Something like:

      //==File: header.h
      #pragma once
      #include extern int variable;
      //==end header.h

      //==File: main.cpp
      #include "header.h"

      int variable = 0;

      void main()
      {
      // the rest of your code
      }

      J 1 Reply Last reply
      0
      • L Lost User

        You should declare it extern in the header, and define it in only one of your .cpp files. Something like:

        //==File: header.h
        #pragma once
        #include extern int variable;
        //==end header.h

        //==File: main.cpp
        #include "header.h"

        int variable = 0;

        void main()
        {
        // the rest of your code
        }

        J Offline
        J Offline
        Javier Luis Lopez
        wrote on last edited by
        #3

        It works! It must be "declared" as extern in the header file one time and "defined" one time as you said in the main.cpp. It must not be defined again at any other file (source.cpp) unless extern is added The dafault value must be placed in the main.cpp as you wrote I do not like the solution very much because I have to be careful when changing the name of the variable to do in both sides.

        L 1 Reply Last reply
        0
        • J Javier Luis Lopez

          It works! It must be "declared" as extern in the header file one time and "defined" one time as you said in the main.cpp. It must not be defined again at any other file (source.cpp) unless extern is added The dafault value must be placed in the main.cpp as you wrote I do not like the solution very much because I have to be careful when changing the name of the variable to do in both sides.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Member 11988283 wrote:

          It must not be defined again at any other file (source.cpp) unless extern is added

          You do not need to do that, as it is declared in the header.

          Member 11988283 wrote:

          I do not like the solution very much because ...

          Sorry, but you have no choice if you want to use global variables; which is something you should really avoid.

          J 1 Reply Last reply
          0
          • L Lost User

            Member 11988283 wrote:

            It must not be defined again at any other file (source.cpp) unless extern is added

            You do not need to do that, as it is declared in the header.

            Member 11988283 wrote:

            I do not like the solution very much because ...

            Sorry, but you have no choice if you want to use global variables; which is something you should really avoid.

            J Offline
            J Offline
            Javier Luis Lopez
            wrote on last edited by
            #5

            I tried it by delete from the .cpp but unfortunately following error appeared:

            error LNK2001: external symbol "int variable" (?variable@@3HA) unresolved

            I agree with you, there is not other choice. I do not like also global variables so I had not that problem before :)

            L 1 Reply Last reply
            0
            • J Javier Luis Lopez

              I tried it by delete from the .cpp but unfortunately following error appeared:

              error LNK2001: external symbol "int variable" (?variable@@3HA) unresolved

              I agree with you, there is not other choice. I do not like also global variables so I had not that problem before :)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Member 11988283 wrote:

              I tried it by delete from the .cpp

              Why? I showed you what to do and explained where it should be declared: in the header. And where it should be defined: in one .cpp file.

              1 Reply Last reply
              0
              • J Javier Luis Lopez

                I tried to isolate the problem It appears when a global variable is declared at the .h header file and the header file is called from 2 or more cpp files, as example I tried to link the following 3 files:

                //==File: header.h
                #pragma once
                #include int variable;
                //==end header.h

                //==File: main.cpp
                #include "header.h"
                void main()
                {
                variable=0;
                }
                //==end main.h

                //File: source2.cpp
                #include "header.h"

                void change()
                {
                variable=0;
                }
                //==end source2.cpp

                Then the following LINKER error appear: 1>source2.obj : error LNK2005: redefined "int variable" (?variable@@3HA) in main.obj The only way I could "fix" the problem is in project properties> linker>command line add: /FORCE:MULTIPLE Unfortunately it can hide possible problems I think the problem is that the #pragma once does not work. It does not work also to define a constant and use #ifndef to avoid reading 2 times.

                D Offline
                D Offline
                Daniel Pfeffer
                wrote on last edited by
                #7

                Another way to solve this problem (and have only one place where the variable is defined): In foo.h:

                #ifdef DEFINE_GLOBALS
                #define GLOBAL
                #define INIT(x) = (x)
                #else
                #define GLOBAL extern
                #define INIT(x)
                #endif

                GLOBAL int globalInt INIT(42);

                In foo.cpp (and ONLY in foo.cpp):

                #define DEFINE_GLOBALS
                #include "foo.h"

                In other source files:

                #include "foo.h"

                If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                CPalliniC L 2 Replies Last reply
                0
                • D Daniel Pfeffer

                  Another way to solve this problem (and have only one place where the variable is defined): In foo.h:

                  #ifdef DEFINE_GLOBALS
                  #define GLOBAL
                  #define INIT(x) = (x)
                  #else
                  #define GLOBAL extern
                  #define INIT(x)
                  #endif

                  GLOBAL int globalInt INIT(42);

                  In foo.cpp (and ONLY in foo.cpp):

                  #define DEFINE_GLOBALS
                  #include "foo.h"

                  In other source files:

                  #include "foo.h"

                  If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #8

                  I don't see the advantage of this over the usual approach (the one suggested by Richard).

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • D Daniel Pfeffer

                    Another way to solve this problem (and have only one place where the variable is defined): In foo.h:

                    #ifdef DEFINE_GLOBALS
                    #define GLOBAL
                    #define INIT(x) = (x)
                    #else
                    #define GLOBAL extern
                    #define INIT(x)
                    #endif

                    GLOBAL int globalInt INIT(42);

                    In foo.cpp (and ONLY in foo.cpp):

                    #define DEFINE_GLOBALS
                    #include "foo.h"

                    In other source files:

                    #include "foo.h"

                    If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    The problem with this is that everything is hidden by those #defines so as to make it less easy to understand when looking at the source code.

                    D 1 Reply Last reply
                    0
                    • L Lost User

                      The problem with this is that everything is hidden by those #defines so as to make it less easy to understand when looking at the source code.

                      D Offline
                      D Offline
                      Daniel Pfeffer
                      wrote on last edited by
                      #10

                      It can be confusing if encountered for the first time, but if this is the agreed idiom in your team, it ensures that all definitions occur only once. This in turn eliminates all possibilities of mismatches between declarations and definitions.

                      If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack. --Winston Churchill

                      1 Reply Last reply
                      0
                      • J Javier Luis Lopez

                        I tried to isolate the problem It appears when a global variable is declared at the .h header file and the header file is called from 2 or more cpp files, as example I tried to link the following 3 files:

                        //==File: header.h
                        #pragma once
                        #include int variable;
                        //==end header.h

                        //==File: main.cpp
                        #include "header.h"
                        void main()
                        {
                        variable=0;
                        }
                        //==end main.h

                        //File: source2.cpp
                        #include "header.h"

                        void change()
                        {
                        variable=0;
                        }
                        //==end source2.cpp

                        Then the following LINKER error appear: 1>source2.obj : error LNK2005: redefined "int variable" (?variable@@3HA) in main.obj The only way I could "fix" the problem is in project properties> linker>command line add: /FORCE:MULTIPLE Unfortunately it can hide possible problems I think the problem is that the #pragma once does not work. It does not work also to define a constant and use #ifndef to avoid reading 2 times.

                        _ Offline
                        _ Offline
                        _Superman_
                        wrote on last edited by
                        #11

                        The basis for the error is that building an EXE constitutes compiling and linking. Compilation - Every CPP file, also called a translation unit or compilation unit, is compiled separately to form OBJ files. Linking - All the OBJ files are linked together to become the EXE. In your case, during compilation, every OBJ file will contain a symbol called int variable. This means the compilation of all units are successful. But when the linker takes all the OBJ files to create an EXE, it finds the same symbol in all translation units and so it becomes ambiguous to it as to which symbol to use. Having said this, there are two things that you may want to do with global variables. First You may want to use a global variable that is shared between all translation units. This is the answer that you've gotten so far. The global variable declaration must happen only once so that the linker only finds one symbol for the variable and links all other extern references to that symbol. Second You may want to use the same name for the global variable in each translation unit. For this to happen, declare the global variable as static - static int variable; This forces the compilation process to not share the symbol among other translation units. For more information refer to this link - Types of Linkage[^]

                        «_Superman_»  _I love work. It gives me something to do between weekends.

                        _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                        Polymorphism in C

                        L 1 Reply Last reply
                        0
                        • _ _Superman_

                          The basis for the error is that building an EXE constitutes compiling and linking. Compilation - Every CPP file, also called a translation unit or compilation unit, is compiled separately to form OBJ files. Linking - All the OBJ files are linked together to become the EXE. In your case, during compilation, every OBJ file will contain a symbol called int variable. This means the compilation of all units are successful. But when the linker takes all the OBJ files to create an EXE, it finds the same symbol in all translation units and so it becomes ambiguous to it as to which symbol to use. Having said this, there are two things that you may want to do with global variables. First You may want to use a global variable that is shared between all translation units. This is the answer that you've gotten so far. The global variable declaration must happen only once so that the linker only finds one symbol for the variable and links all other extern references to that symbol. Second You may want to use the same name for the global variable in each translation unit. For this to happen, declare the global variable as static - static int variable; This forces the compilation process to not share the symbol among other translation units. For more information refer to this link - Types of Linkage[^]

                          «_Superman_»  _I love work. It gives me something to do between weekends.

                          _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                          Polymorphism in C

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          «_Superman_» wrote:

                          For this to happen, declare the global variable as static - static int variable;

                          Which means they are not global.

                          _ 1 Reply Last reply
                          0
                          • L Lost User

                            «_Superman_» wrote:

                            For this to happen, declare the global variable as static - static int variable;

                            Which means they are not global.

                            _ Offline
                            _ Offline
                            _Superman_
                            wrote on last edited by
                            #13

                            They are definitely not local. They are global to the functions in that file. They are local to that file. If you look at the assembly output, they are created as global variables.

                            «_Superman_»  _I love work. It gives me something to do between weekends.

                            _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                            Polymorphism in C

                            L 1 Reply Last reply
                            0
                            • _ _Superman_

                              They are definitely not local. They are global to the functions in that file. They are local to that file. If you look at the assembly output, they are created as global variables.

                              «_Superman_»  _I love work. It gives me something to do between weekends.

                              _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                              Polymorphism in C

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              «_Superman_» wrote:

                              They are definitely not local.

                              Yes they are; go back to the link in your original post and read it again.

                              «_Superman_» wrote:

                              They are global to the functions in that file.

                              The term 'global' here is confusing the issue.

                              «_Superman_» wrote:

                              They are local to that file.

                              So which is it?

                              «_Superman_» wrote:

                              If you look at the assembly output, they are created as global variables.

                              Again you misunderstand the term 'global'.

                              _ 1 Reply Last reply
                              0
                              • L Lost User

                                «_Superman_» wrote:

                                They are definitely not local.

                                Yes they are; go back to the link in your original post and read it again.

                                «_Superman_» wrote:

                                They are global to the functions in that file.

                                The term 'global' here is confusing the issue.

                                «_Superman_» wrote:

                                They are local to that file.

                                So which is it?

                                «_Superman_» wrote:

                                If you look at the assembly output, they are created as global variables.

                                Again you misunderstand the term 'global'.

                                _ Offline
                                _ Offline
                                _Superman_
                                wrote on last edited by
                                #15

                                It is a global variable with file scope.

                                «_Superman_»  _I love work. It gives me something to do between weekends.

                                _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                                Polymorphism in C

                                L 1 Reply Last reply
                                0
                                • _ _Superman_

                                  It is a global variable with file scope.

                                  «_Superman_»  _I love work. It gives me something to do between weekends.

                                  _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                                  Polymorphism in C

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  It is not "global"; You are just confusing the issue by saying that.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups