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. Linkage error - Variable already defined

Linkage error - Variable already defined

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 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.
  • P Offline
    P Offline
    piul
    wrote on last edited by
    #1

    This problem probably sounds silly but I can't get my head round it; there it goes:

    #include <DelimFileParser.h>
    int a; //I can't define any type of variable in here
    typedef struct
    {
    std::string m_devTrainId;
    std::string m_devIp;//sockaddr_in
    ...

    The compiler's message:

    Linking...
    CDeviceFileParser.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
    CSnapLogger.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
    snaplogger_main.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj

    snaplogger_main.h includes CSnapLogger.h CSnapLogger.h includes CCommsManager.h CCommsManager.h includes CDeviceFileParser.h

    L _ 2 Replies Last reply
    0
    • P piul

      This problem probably sounds silly but I can't get my head round it; there it goes:

      #include <DelimFileParser.h>
      int a; //I can't define any type of variable in here
      typedef struct
      {
      std::string m_devTrainId;
      std::string m_devIp;//sockaddr_in
      ...

      The compiler's message:

      Linking...
      CDeviceFileParser.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
      CSnapLogger.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
      snaplogger_main.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj

      snaplogger_main.h includes CSnapLogger.h CSnapLogger.h includes CCommsManager.h CCommsManager.h includes CDeviceFileParser.h

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

      If you want to use this variable only in this source module then add the static keyword thus:

      static int a;

      Just say 'NO' to evaluated arguments for diadic functions! Ash

      P 1 Reply Last reply
      0
      • L Lost User

        If you want to use this variable only in this source module then add the static keyword thus:

        static int a;

        Just say 'NO' to evaluated arguments for diadic functions! Ash

        P Offline
        P Offline
        piul
        wrote on last edited by
        #3

        Thank you!

        1 Reply Last reply
        0
        • P piul

          This problem probably sounds silly but I can't get my head round it; there it goes:

          #include <DelimFileParser.h>
          int a; //I can't define any type of variable in here
          typedef struct
          {
          std::string m_devTrainId;
          std::string m_devIp;//sockaddr_in
          ...

          The compiler's message:

          Linking...
          CDeviceFileParser.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
          CSnapLogger.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj
          snaplogger_main.obj : error LNK2005: "int a" (?a@@3HA) already defined in CCommsManager.obj

          snaplogger_main.h includes CSnapLogger.h CSnapLogger.h includes CCommsManager.h CCommsManager.h includes CDeviceFileParser.h

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

          If you want a global variable to be accessed from all files, make the definition in the source file (.c, .cpp etc.) instead of a header file. In the header do a declaration like this - extern int a;

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

          Microsoft MVP (Visual C++)

          Polymorphism in C

          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