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. link error.

link error.

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
5 Posts 4 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
    Kiran Pinjala
    wrote on last edited by
    #1

    i have a

    CSocket SomeSocket;

    variable declared globally in a "one.h" file.I have inlcuded this one.h in one.cpp as

    #include "one.h"

    I used this SomeSocket in another two.h files where i have included the file

    #include "one.h"

    now i get a linker error stating

    two.obj : error LNK2005: "CSocket SomeSocket" (?xst@@3UGlobals@@A)
    already defined in one.obj

    what was the mistake. -- modified at 6:54 Friday 30th November, 2007

    Adi Vishayam

    C C I 3 Replies Last reply
    0
    • K Kiran Pinjala

      i have a

      CSocket SomeSocket;

      variable declared globally in a "one.h" file.I have inlcuded this one.h in one.cpp as

      #include "one.h"

      I used this SomeSocket in another two.h files where i have included the file

      #include "one.h"

      now i get a linker error stating

      two.obj : error LNK2005: "CSocket SomeSocket" (?xst@@3UGlobals@@A)
      already defined in one.obj

      what was the mistake. -- modified at 6:54 Friday 30th November, 2007

      Adi Vishayam

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

      You need to put include guards in your header file: #ifndef YOURFILE #define YOURFILE // Code here #endif (replace YOURFILE by something unique, like the file name). You could also use #pragma once for microsoft compiler.


      Cédric Moonen Software developer
      Charting control [v1.2]

      K 1 Reply Last reply
      0
      • C Cedric Moonen

        You need to put include guards in your header file: #ifndef YOURFILE #define YOURFILE // Code here #endif (replace YOURFILE by something unique, like the file name). You could also use #pragma once for microsoft compiler.


        Cédric Moonen Software developer
        Charting control [v1.2]

        K Offline
        K Offline
        Kiran Pinjala
        wrote on last edited by
        #3

        Cedric Moonen wrote:

        You could also use #pragma once for microsoft compiler

        #pragama once is already added. problem remains the same.

        Adi Vishayam

        1 Reply Last reply
        0
        • K Kiran Pinjala

          i have a

          CSocket SomeSocket;

          variable declared globally in a "one.h" file.I have inlcuded this one.h in one.cpp as

          #include "one.h"

          I used this SomeSocket in another two.h files where i have included the file

          #include "one.h"

          now i get a linker error stating

          two.obj : error LNK2005: "CSocket SomeSocket" (?xst@@3UGlobals@@A)
          already defined in one.obj

          what was the mistake. -- modified at 6:54 Friday 30th November, 2007

          Adi Vishayam

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          you have to declare it extern inside your include file (i.e. "one.h")

          extern CSocket SomeSocket;

          and then define once inside a single source file (e.g. in your "one.cpp").

          CSocket SomeSocket;

          The mistake is in your include file: as it stands CSocket SomeSocket is defined in multiple source files. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

          1 Reply Last reply
          0
          • K Kiran Pinjala

            i have a

            CSocket SomeSocket;

            variable declared globally in a "one.h" file.I have inlcuded this one.h in one.cpp as

            #include "one.h"

            I used this SomeSocket in another two.h files where i have included the file

            #include "one.h"

            now i get a linker error stating

            two.obj : error LNK2005: "CSocket SomeSocket" (?xst@@3UGlobals@@A)
            already defined in one.obj

            what was the mistake. -- modified at 6:54 Friday 30th November, 2007

            Adi Vishayam

            I Offline
            I Offline
            Iain Clarke Warrior Programmer
            wrote on last edited by
            #5

            CPallini has given you the correct answer. But I see this mistake a lot with novice programmers. The linker is complaining because (as it says) you have two instances of SomeSocket. Before the compiler attacks you "one.c" file, a preprocessor attacks it, and processes the includes. An easy exercise for you: remove the #include from one.c, and cut and paste the whole of one.h into the top of one.c. Do this for two.c. Now have a lightbulb moment as you realise what the linker is telling you. Iain.

            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