I've tried reading all I can on this subject, but I'm really stuck with this
problem.
I have a singleton class as follows in a dll(dll#1)... (DLL_API is the
normal dllexport/dllimport define)
template <class T> class DLL_API singleton
{
public:
static T * instance(void) { return m_instance; }
...
private:
static T * m_instance;
};
and I use it as a base for another class in the same dll(dll#1)...
in myclass.hpp
class DLL_API myclass : public singleton<myclass>
{
...
};
in myclass.cpp
DLL_API myclass* singleton<myclass>::m_instance;
This dll#1 compiles and links fine.
But when I try to use a myclass object from another dll(dll#2) I get the
following link error.
error LNK2001: unresolved external symbol "private: static class myclass *
singleton<class myclass>::m_instance"
I include myclass.hpp in the dll#2 project.
So how do I resolve this link error? I have been at this forever and can't
figure it out.
Thank you,
Andrew
--
Cheers
Check Abdoul
----------------
"Andrew K" <an...@anon.com> wrote in message
news:u7FAPwNNCHA.360@tkmsftngp13...
extern template class DLL_API singleton<myclass>;
for each class using singleton you want to export from the DLL.
--
With best wishes,
Igor Tandetnik
"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken
"Andrew K" <an...@anon.com> wrote in message
news:u7FAPwNNCHA.360@tkmsftngp13...
Neil Groves
"Igor Tandetnik" <itand...@whenu.com> wrote in message
news:#RSMQEONCHA.2404@tkmsftngp11...
"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken
"Neil Groves" <neilgrov...@dial.pipex.com> wrote in message
news:OExhmaONCHA.2032@tkmsftngp08...
It depends what the gentleman is trying to achieve. If he is simply after
an encapsulation for a singleton, then rather than put it into a DLL at all
he could just reuse a header file implement and export the derived class
without exporting the base singleton class at all.
Or is that problematic too? I hope not, because that's what I'm doing here,
and it seems to work.
Neil Groves
"Igor Tandetnik" <itand...@whenu.com> wrote in message
news:usXsiqONCHA.2028@tkmsftngp10...
Now I'm writing DLL1.dll that links against Singleton.dll and uses
singleton<myclass>::instance. Since singleton<myclass>::my_instance is
not exported from Singleton.dll, the compiler has to allocate it inside
DLL1.dll to satisfy the linker.
Then I'm writing DLL2.dll that also links against Singleton.dll and also
uses singleton<myclass>::instance. By the same token, it too gets a copy
of singleton<myclass>::my_instance.
Finally, I write an EXE that uses both DLL1.dll and DLL2.dll. I'm
getting myclass pointer from DLL1, setting some values through it, and
expect DLL2 to see those changes. But DLL2 has its own pointer and its
own instance of myclass - a "singleton" is not single anymore.
--
With best wishes,
Igor Tandetnik
"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken
"Neil Groves" <neilgrov...@dial.pipex.com> wrote in message
news:#lDKZLPNCHA.1996@tkmsftngp12...
I have DLL1 and DLL2
In DLL1 I specify DLL_API to be __declspec(dllexport) and EXPIMP_TEMPLATE to
be null.
In DLL2 I specify DLL_API to be __declspec(dllimport) and EXPIMP_TEMPLATE to
be extern.
I'm using DLL1 stuff in DLL2.
So as per my original example using the singleton I do this... (myclass is
part of DLL1 and I want to use it in DLL2)
Right after this...
class DLL_API myclass : public singleton<myclass>
{
...
};
I do this...
EXPIMP_TEMPLATE template class DLL_API singleton<myclass>;
But now I get the following compiler error from this newly added line...
error C2059: syntax error : 'constant'
What in the heck does this mean? I don't use the word 'constant' anywhere
here.
If I remove the EXPIMP_TEMPLATE part everything compiles and links fine, but
I get this warning...
warning C4661: 'myclass *singleton<myclass>::m_instance' : no suitable
definition provided for explicit template instantiation request
f:\path_to_file\singleton.hpp(53) : see declaration of 'm_instance'
and double clicking on it takes me to line 261 in xtree
So what does that mean and can I safely ignore it?
Thanks for the help,
Andrew K
"Igor Tandetnik" <itand...@whenu.com> wrote in message
news:#RSMQEONCHA.2404@tkmsftngp11...
"Andrew K" <an...@anon.com> wrote in message
news:O2iWfzPNCHA.1900@tkmsftngp11...
As Igor pointed out, you cannot export a template from a DLL - you can only
export template instantiations.
What you're in effect asking the compiler/linker to do is to export from
DLL1 a class which cannot be created anywhere but in DLL2 (think about it -
HOW on Earth would it export a definition for singleton<MyClass>::m_instance
from DLL1 when MyClass is defined in DLL2?).
-cd
"Andrew K" <an...@anon.com> wrote in message
news:u7FAPwNNCHA.360@tkmsftngp13...
So it's the singleton<MyClass> that I want to be exported to DLL2.
When I said I wanted to use MyClass in DLL2, that what I meant... use the
exported definition from DLL1 in DLL2.
I understand though what you are saying and can see how that wouldn't work.
Thanks
"Carl Daniel" <cpda...@pacbell.net> wrote in message
news:uTnUoPQNCHA.2104@tkmsftngp08...
Ah yes - I should have read more carefully!
Does this help?
// singleton.h
template <class T> class singleton
{
public:
static T * instance(void) { return m_instance; }
private:
static T * m_instance;
};
class DLL_API MyClass : public singleton<MyClass>
{
};
// singleton.cpp
#include "singleton.h"
MyClass* singleton<MyClass>::m_instance;
// use_singleton.cpp
#include "singleton.h"
void foo()
{
MyClass* p = MyClass::instance();
}
cl /DDLL_API=__declspec(dllexport) /LD /MD singleton.cpp
cl /DDLL_API=__declspec(dllimport) /LD /MD use_singleton.cpp singleton.lib
dumpbin /exports singleton.dll
Dump of file singleton.dll
File Type: DLL
ordinal hint RVA name
1 0 00001000 ??4?$singleton@VMyClass@@@@QAEAAV0@ABV0@@Z
2 1 00001000 ??4MyClass@@QAEAAV0@ABV0@@Z
3 2 00001020 ?instance@?$singleton@VMyClass@@@@SAPAVMyClass@@XZ
4 3 00003010 ?m_instance@?$singleton@VMyClass@@@@0PAVMyClass@@A
Dump of file use_singleton.dll
File Type: DLL
singleton.dll
10002028 Import Address Table
100020D4 Import Name Table
0 time date stamp
0 Index of first forwarder reference
2 ?instance@?$singleton@VMyClass@@@@SAPAVMyClass@@XZ
Very true, but that is often what I want. It is a singleton per client.
The definition of singleton is not so unambiguous. For example, even with
your example if you have two client processes (EXE's) you get two instances,
but that does not make your example wrong either. You could write a
singleton for a DLL instance, per process, per client machine, or per
network. They are still singletons I think?
We just implement different singletons, for different needs.
Neil Groves
"Igor Tandetnik" <itand...@whenu.com> wrote in message
news:u4NXbZPNCHA.2488@tkmsftngp12...
Neil Groves
PS. Just pulling your leg Igor
"Carl Daniel" <cpda...@pacbell.net> wrote in message
news:#s9f9YRNCHA.2004@tkmsftngp13...
"Carl Daniel" <cpda...@pacbell.net> wrote in message
news:#s9f9YRNCHA.2004@tkmsftngp13...
> "Andrew Kaspick" <an...@anon.com> wrote in message
> news:53m09.55220$f05.2...@news1.calgary.shaw.ca...
> Ah yes - I should have read more carefully!
>
> Does this help?
>
> // singleton.h
<snip>