Linker Error

Discussion in 'C++' started by siddhu, Mar 3, 2008.

  1. siddhu

    siddhu Guest

    Dear Experts,

    I am getting a linker error in VC7.

    I have written a singleton

    //SysInfoFileManager.h

    namespace SysInfo
    {
    class SysInfoFileManager
    {
    SysInfoFileManager(){}
    SysInfoFileManager(const SysInfoFileManager&);
    ~SysInfoFileManager(){}
    SysInfoFileManager& operator=(const SysInfoFileManager&);
    static SysInfoFileManager* _pinstance;
    public:
    static SysInfoFileManager* instance();
    //void trim(std::string& str);
    };
    }

    SysInfo::SysInfoFileManager* SysInfo::SysInfoFileManager::_pinstance =
    0;

    //SysInfoFileManager.cpp

    #include "SysInfoFileManager.h"

    namespace SysInfo
    {
    SysInfoFileManager* SysInfoFileManager::instance()
    {
    if (!_pinstance)
    {
    //Lock Mutex
    if (!_pinstance)
    {
    static SysInfoFileManager sysInfoFileManager;
    _pinstance = &sysInfoFileManager;
    }
    }
    return _pinstance;
    }
    }

    I am getting follwing error. Any suggestion would of great help.

    Error 1 error LNK2005: "private: static class
    SysInfo::SysInfoFileManager *
    SysInfo::SysInfoFileManager::_pinstance" (?
    _pinstance@SysInfoFileManager@SysInfo@@$$Q0PAV12@A) already defined in
    SyinfoWebServerClass.obj SysInfoFileManager.obj

    Error 2 fatal error LNK1169: one or more multiply defined symbols
    found C:\Webserver\SyinfoWebServer\Debug\SyinfoWebServer.dll 1


    Thanks
    siddhu
    siddhu, Mar 3, 2008
    #1
    1. Advertising

  2. siddhu

    Martin York Guest

    Move this line

    > SysInfo::SysInfoFileManager* SysInfo::SysInfoFileManager::_pinstance = 0;


    Into the cpp file.
    Martin York, Mar 3, 2008
    #2
    1. Advertising

  3. On Mar 3, 9:43 pm, siddhu <> wrote:
    > Dear Experts,
    >
    > I am getting a linker error in VC7.
    >
    > I have written a singleton
    >
    > //SysInfoFileManager.h
    >
    > namespace SysInfo
    > {
    >         class SysInfoFileManager
    >         {
    >                 SysInfoFileManager(){}
    >                 SysInfoFileManager(const SysInfoFileManager&);
    >                 ~SysInfoFileManager(){}
    >                 SysInfoFileManager& operator=(const SysInfoFileManager&);
    >                 static SysInfoFileManager* _pinstance;
    >         public:
    >                 static SysInfoFileManager* instance();
    >                 //void trim(std::string& str);
    >         };
    >
    > }
    >
    > SysInfo::SysInfoFileManager* SysInfo::SysInfoFileManager::_pinstance =
    > 0;
    >
    > //SysInfoFileManager.cpp
    >
    > #include "SysInfoFileManager.h"
    >
    > namespace SysInfo
    > {
    >         SysInfoFileManager* SysInfoFileManager::instance()
    >         {
    >                 if (!_pinstance)
    >                 {
    >                         //Lock Mutex
    >                         if (!_pinstance)
    >                         {
    >                                 static SysInfoFileManager sysInfoFileManager;
    >                                 _pinstance = &sysInfoFileManager;
    >                         }
    >                 }
    >                 return _pinstance;
    >         }
    >
    > }
    >
    > I am getting follwing error. Any suggestion would of great help.
    >
    > Error   1       error LNK2005: "private: static class
    > SysInfo::SysInfoFileManager *
    > SysInfo::SysInfoFileManager::_pinstance" (?
    > _pinstance@SysInfoFileManager@SysInfo@@$$Q0PAV12@A) already defined in
    > SyinfoWebServerClass.obj        SysInfoFileManager.obj
    >
    > Error   2       fatal error LNK1169: one or more multiply defined symbols
    > found   C:\Webserver\SyinfoWebServer\Debug\SyinfoWebServer.dll  1
    >
    > Thanks
    > siddhu


    Put this into the .cpp file:

    SysInfo::SysInfoFileManager*
    SysInfo::SysInfoFileManager::_pinstance = 0;

    You are including SysInfoFileManager.h in SyinfoWebServerClass.h (and
    hence SyinfoWebServerClass.cpp) and that causes re-definition. statics
    must be defined only once.
    Abhishek Padmanabh, Mar 3, 2008
    #3
  4. siddhu

    James Kanze Guest

    On Mar 3, 5:43 pm, siddhu <> wrote:
    > I am getting a linker error in VC7.


    > I have written a singleton


    > //SysInfoFileManager.h


    > namespace SysInfo
    > {
    > class SysInfoFileManager
    > {
    > SysInfoFileManager(){}
    > SysInfoFileManager(const SysInfoFileManager&);
    > ~SysInfoFileManager(){}
    > SysInfoFileManager& operator=(const SysInfoFileManager&);
    > static SysInfoFileManager* _pinstance;
    > public:
    > static SysInfoFileManager* instance();
    > //void trim(std::string& str);
    > };
    > }


    > SysInfo::SysInfoFileManager* SysInfo::SysInfoFileManager::_pinstance =
    > 0;


    The above is a definition. It may only occur once in the
    program. Which means that it doesn't belong in the header, but
    in the source file.

    > //SysInfoFileManager.cpp
    >
    > #include "SysInfoFileManager.h"
    >
    > namespace SysInfo
    > {
    > SysInfoFileManager* SysInfoFileManager::instance()
    > {
    > if (!_pinstance)
    > {
    > //Lock Mutex


    Should I assume from this comment that the code is designed for
    a multithreaded environment. If so, the lock here is too late.
    You need to lock before the access to _pinstance above for the
    code to work.

    > if (!_pinstance)
    > {
    > static SysInfoFileManager sysInfoFileManager;
    > _pinstance = &sysInfoFileManager;
    > }
    > }
    > return _pinstance;
    > }
    > }


    > I am getting follwing error. Any suggestion would of great help.


    > Error 1 error LNK2005: "private: static class
    > SysInfo::SysInfoFileManager *
    > SysInfo::SysInfoFileManager::_pinstance" (?
    > _pinstance@SysInfoFileManager@SysInfo@@$$Q0PAV12@A) already defined in
    > SyinfoWebServerClass.obj SysInfoFileManager.obj


    > Error 2 fatal error LNK1169: one or more multiply defined symbols
    > found C:\Webserver\SyinfoWebServer\Debug\SyinfoWebServer.dll 1


    Move the definition of the static variable to the source file.
    And move the lock so that it protects *all* accesses to
    _pinstance. (Which of course means that the two accesses aren't
    necessary.)

    You might also want to avoid names starting with an underscore.
    The standard says you're safe as long as the next character is a
    small letter, and the name isn't at global scope, but in
    practice, I've found a lot of macros in system headers which
    started with an underscore, followed by a small letter. (The
    other convention to avoid in your own code is all caps.) My own
    convention is to use the prefix "my" for member variables (and
    "our" for static members), another common convention is "m_",
    and sometimes "s_", to distinguish static members from the
    others. (In theory, if you're naming things well, you don't
    need any convention. This is one case, however, where my
    experience doesn't seem to agree with the theory.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 4, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wolfgang Rueckert

    Linker error - unsolved external symbol

    Wolfgang Rueckert, Jun 30, 2003, in forum: C++
    Replies:
    0
    Views:
    686
    Wolfgang Rueckert
    Jun 30, 2003
  2. Martin Magnusson

    Linker error with static const field

    Martin Magnusson, Oct 20, 2003, in forum: C++
    Replies:
    2
    Views:
    432
    Ron Natalie
    Oct 20, 2003
  3. Arne Schmitz
    Replies:
    3
    Views:
    348
    E. Robert Tisdale
    Apr 16, 2004
  4. TR
    Replies:
    3
    Views:
    8,125
    New_user
    Jun 10, 2004
  5. quad
    Replies:
    3
    Views:
    2,285
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Jun 9, 2007
Loading...

Share This Page