Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dlls and static/template exports

126 views
Skip to first unread message

Andrew K

unread,
Jul 26, 2002, 3:39:38 PM7/26/02
to
Hello,

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


CheckAbdoul

unread,
Jul 26, 2002, 4:18:03 PM7/26/02
to
See if KB article
Q168958 - HOWTO: Exporting STL Components Inside & Outside
of a Class
helps you.

--
Cheers
Check Abdoul
----------------

"Andrew K" <an...@anon.com> wrote in message
news:u7FAPwNNCHA.360@tkmsftngp13...

Igor Tandetnik

unread,
Jul 26, 2002, 4:15:27 PM7/26/02
to
You cannot export a template from the DLL. You can export a particular
instantiation of this template. See KB Article Q168958 "HOWTO: Exporting
STL Components Inside & Outside of a Class". You need something like

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

unread,
Jul 26, 2002, 4:55:13 PM7/26/02
to
Therefore for this example of a singleton base class, it is probably worth
implementing the simple methods inline and sticking it in a common header
file.

Neil Groves

"Igor Tandetnik" <itand...@whenu.com> wrote in message
news:#RSMQEONCHA.2404@tkmsftngp11...

Igor Tandetnik

unread,
Jul 26, 2002, 5:23:59 PM7/26/02
to
I don't think that's going to fly with static data. You do really want
exactly one instance of singleton<myclass>::m_instance exported from the
DLL rather than having each client module create its own instance -
that's the whole point of the exercise, after all. So you need to
explicitly export either this one data member for each template
instantiation, or the whole class (which will end up exporting data
member).

--
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:OExhmaONCHA.2032@tkmsftngp08...

Neil Groves

unread,
Jul 26, 2002, 6:22:32 PM7/26/02
to
Igor,

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

Igor Tandetnik

unread,
Jul 26, 2002, 6:47:53 PM7/26/02
to
Suppose you have Singleton.dll exporting myclass. The question here is
whether or not Singleton.dll also exports
singleton<myclass>::my_instance. Let's assume it does not - it will not
unless you explicitly ask, and you insist that it's OK not to ask.

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

Andrew K

unread,
Jul 26, 2002, 7:34:31 PM7/26/02
to
Ok, this is what I've tried and this is the error/warning that I'm
getting...

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 Kaspick

unread,
Jul 26, 2002, 7:53:16 PM7/26/02
to
The error below occurs when compiling DLL1. DLL2 compiles fine, but I do
get the warning "C4231: nonstandard extension used : 'extern' before
template explicit instantiation", which I've read I can safely ignore.

"Andrew K" <an...@anon.com> wrote in message

news:O2iWfzPNCHA.1900@tkmsftngp11...

Carl Daniel

unread,
Jul 26, 2002, 8:24:52 PM7/26/02
to
You MUST change your design.

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

Andrew Kaspick

unread,
Jul 26, 2002, 8:45:53 PM7/26/02
to
MyClass *is* defined in DLL1 along with the singleton template.

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

Carl Daniel

unread,
Jul 26, 2002, 10:36:04 PM7/26/02
to
"Andrew Kaspick" <an...@anon.com> wrote in message
news:53m09.55220$f05.2...@news1.calgary.shaw.ca...

> MyClass *is* defined in DLL1 along with the singleton template.
>
> 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

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


Neil Groves

unread,
Jul 26, 2002, 11:15:56 PM7/26/02
to
Igor,

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

unread,
Jul 26, 2002, 11:18:18 PM7/26/02
to
Yes exactly as I suggested earlier... don't let Igor see ;-)

Neil Groves
PS. Just pulling your leg Igor


"Carl Daniel" <cpda...@pacbell.net> wrote in message

news:#s9f9YRNCHA.2004@tkmsftngp13...

Andrew Kaspick

unread,
Jul 27, 2002, 12:31:50 AM7/27/02
to
Yes, thank you. That helps.

"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>


0 new messages