Linker Error

S

siddhu

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
 
A

Abhishek Padmanabh

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.
 
J

James Kanze

I am getting a linker error in VC7.
I have written a singleton

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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top