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 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 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
        #9

        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
        • 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
          #10

          No.

          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