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. Linking problem

Linking problem

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
16 Posts 6 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.
  • K Offline
    K Offline
    KASR1
    wrote on last edited by
    #1

    i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?

    C L K S J 5 Replies Last reply
    0
    • K KASR1

      i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Why do you put the definition of your function in a header file ? Usually it is much better practice to move it to a cpp file. Anyway, what you can do to remove your problem is make use of include guards. Surround your code in the file with this (replace MYFILE by something unique, typically the file name):

      #ifndef MYFILE
      #define MYFILE

      .... // The rest of the header comes here

      #endif

      You can also put #pragma once at the top of your file. Google for "include guards" for more information.

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      K 1 Reply Last reply
      0
      • C Cedric Moonen

        Why do you put the definition of your function in a header file ? Usually it is much better practice to move it to a cpp file. Anyway, what you can do to remove your problem is make use of include guards. Surround your code in the file with this (replace MYFILE by something unique, typically the file name):

        #ifndef MYFILE
        #define MYFILE

        .... // The rest of the header comes here

        #endif

        You can also put #pragma once at the top of your file. Google for "include guards" for more information.

        Cédric Moonen Software developer
        Charting control [v2.0] OpenGL game tutorial in C++

        K Offline
        K Offline
        KASR1
        wrote on last edited by
        #3

        i tried with #pragma once its not working.

        C 1 Reply Last reply
        0
        • K KASR1

          i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?

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

          Don't put implementation code in header files. Place this function in one .cpp file only.

          K 1 Reply Last reply
          0
          • K KASR1

            i tried with #pragma once its not working.

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            Where did you put it ? And what do you mean with "its not working" ? Anyway, as already suggested (by someone else also), putting the implementation of your function in a header file is very bad practice. EDIT: it won't work anyway because if your file is include from two completely different cpp files, then you will end up with a duplicate function at compile time. So, move your implementation in a separate cpp file instead.

            Cédric Moonen Software developer
            Charting control [v2.0] OpenGL game tutorial in C++

            K 2 Replies Last reply
            0
            • L Lost User

              Don't put implementation code in header files. Place this function in one .cpp file only.

              K Offline
              K Offline
              KASR1
              wrote on last edited by
              #6

              For some issues we have included one function definition in header file. That we cannot change. However

              #pragma once

              should slove the problem. But it doesnt why?

              C L 2 Replies Last reply
              0
              • K KASR1

                i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?

                K Offline
                K Offline
                KarstenK
                wrote on last edited by
                #7

                is in Test2 the methode ALSO defined? X|

                Press F1 for help or google it. Greetings from Germany

                K 1 Reply Last reply
                0
                • C Cedric Moonen

                  Where did you put it ? And what do you mean with "its not working" ? Anyway, as already suggested (by someone else also), putting the implementation of your function in a header file is very bad practice. EDIT: it won't work anyway because if your file is include from two completely different cpp files, then you will end up with a duplicate function at compile time. So, move your implementation in a separate cpp file instead.

                  Cédric Moonen Software developer
                  Charting control [v2.0] OpenGL game tutorial in C++

                  K Offline
                  K Offline
                  KASR1
                  wrote on last edited by
                  #8

                  Yes i know that. To solve some issues they added function definition in header file. I have added #pragma at the beginning of header file. Still same error(already defined).

                  1 Reply Last reply
                  0
                  • K KarstenK

                    is in Test2 the methode ALSO defined? X|

                    Press F1 for help or google it. Greetings from Germany

                    K Offline
                    K Offline
                    KASR1
                    wrote on last edited by
                    #9

                    No.

                    1 Reply Last reply
                    0
                    • K KASR1

                      For some issues we have included one function definition in header file. That we cannot change. However

                      #pragma once

                      should slove the problem. But it doesnt why?

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #10

                      KASR1 wrote:

                      For some issues we have included one function definition in header file. That we cannot change.

                      Why ? And why not trying to fix the original issue ?

                      Cédric Moonen Software developer
                      Charting control [v2.0] OpenGL game tutorial in C++

                      K 1 Reply Last reply
                      0
                      • C Cedric Moonen

                        KASR1 wrote:

                        For some issues we have included one function definition in header file. That we cannot change.

                        Why ? And why not trying to fix the original issue ?

                        Cédric Moonen Software developer
                        Charting control [v2.0] OpenGL game tutorial in C++

                        K Offline
                        K Offline
                        KASR1
                        wrote on last edited by
                        #11

                        Actually this project is a static library. When linking with some exe/dll we are getting CTime link error. So we have defined a function with CTime and including that header in the exe/dll.

                        1 Reply Last reply
                        0
                        • K KASR1

                          For some issues we have included one function definition in header file. That we cannot change. However

                          #pragma once

                          should slove the problem. But it doesnt why?

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

                          You do not seem to understand the difference between declaration and implementation. Your header file should contain only the declaration e.g.

                          void TestMethod();

                          The implementation code should go in a single source file (Test.cpp) as follows:

                          void TestMethod()
                          {
                          // body of the TestMethod function
                          }

                          Try reviewing the relevant sections in your C/C++ guides for further explanations.

                          1 Reply Last reply
                          0
                          • K KASR1

                            i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?

                            S Offline
                            S Offline
                            Stuart Dootson
                            wrote on last edited by
                            #13

                            Mark the method as 'inline', or place it inside an anonymous namespace:

                            namespace {
                            void TestMethod()
                            {
                            ..........
                            }
                            }

                            [edit] To expand on that answer - by including a function body in a header, it's defined in lots of .obj files. When you link, the linker attempts to merge contents of all object files into an executable. As you have lots of functions with the same signature, it can't do that. The suggestions I've given prevent that. The inline suggestion should stop the function ever existing in the object file, while ht eanonymous namespace thing stops the function being visible outside each of the object files.[/edit]

                            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                            K 1 Reply Last reply
                            0
                            • S Stuart Dootson

                              Mark the method as 'inline', or place it inside an anonymous namespace:

                              namespace {
                              void TestMethod()
                              {
                              ..........
                              }
                              }

                              [edit] To expand on that answer - by including a function body in a header, it's defined in lots of .obj files. When you link, the linker attempts to merge contents of all object files into an executable. As you have lots of functions with the same signature, it can't do that. The suggestions I've given prevent that. The inline suggestion should stop the function ever existing in the object file, while ht eanonymous namespace thing stops the function being visible outside each of the object files.[/edit]

                              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                              K Offline
                              K Offline
                              KASR1
                              wrote on last edited by
                              #14

                              Yes it solves the linking problem. Its now clear. Thanks.

                              modified on Monday, November 23, 2009 6:33 AM

                              1 Reply Last reply
                              0
                              • C Cedric Moonen

                                Where did you put it ? And what do you mean with "its not working" ? Anyway, as already suggested (by someone else also), putting the implementation of your function in a header file is very bad practice. EDIT: it won't work anyway because if your file is include from two completely different cpp files, then you will end up with a duplicate function at compile time. So, move your implementation in a separate cpp file instead.

                                Cédric Moonen Software developer
                                Charting control [v2.0] OpenGL game tutorial in C++

                                K Offline
                                K Offline
                                KASR1
                                wrote on last edited by
                                #15

                                Please see 'Stuart Dootson' reply.

                                1 Reply Last reply
                                0
                                • K KASR1

                                  i have added a method in a header file as follows void TestMethod() { .......... } Now this header file is included in many places of same project. The following error link error occurs. Test.obj : error LNK2005: "void __cdecl TestMethod(void)" (?TestMethod@@YAXXZ) already defined in Test2.obj How to rectify this error?

                                  J Offline
                                  J Offline
                                  john5632
                                  wrote on last edited by
                                  #16

                                  use \FORCE:MULTIPLE in linker->command

                                  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