Exporting template classes from a DLL ?
Yves Monier -- Yves.Monier@grenoble.rxrc.xerox.com
Tuesday, April 16, 1996
Hello,
My environment is VC++ 4.0 / NT 3.51.
I am trying to build a DLL (32 bits, of course). This DLL has to export many
classes, using the _declspec(dllexport) specifier.
Some of these classes are templates classes. So I thought that something like
the following code should be ok :
template class _declspec(dllexport) Class1 {
// ...
};
The problem is that the compiler produces two errors :
error C2960: template 'class' requires a tag-name
error C2988: unrecognizable template declaration/definition
Of course, if I remove the _declspec(dllexport) specifier, no errors are
produced, but also no .lib file for my DLL ! :-(
The same code compiled without any problem with Borland C++ 4.5 (to produce a
16 bits DLL, with MYEXPORT defined to _export).
Any idea ?
Thanks in advance,
Yves
Chet Murphy -- cmurphy@modelworks.com
Wednesday, April 17, 1996
[Mini-digest: 2 responses]
Yves Monier wrote:
>
> Hello,
>
> My environment is VC++ 4.0 / NT 3.51.
>
> I am trying to build a DLL (32 bits, of course). This DLL has to export many
> classes, using the _declspec(dllexport) specifier.
> Some of these classes are templates classes. So I thought that something like
> the following code should be ok :
>
> template class _declspec(dllexport) Class1 {
> // ...
> };
>
> The problem is that the compiler produces two errors :
> error C2960: template 'class' requires a tag-name
> error C2988: unrecognizable template declaration/definition
>
> Of course, if I remove the _declspec(dllexport) specifier, no errors are
> produced, but also no .lib file for my DLL ! :-(
>
> The same code compiled without any problem with Borland C++ 4.5 (to produce a
> 16 bits DLL, with MYEXPORT defined to _export).
>
> Any idea ?
>
> Thanks in advance,
>
> Yves
Yves,
A template class is built by the compiler not by a linker using object
code. If you want to export a template class you have to provide the
C++ source code.
--Chet Murphy
ModelWorks Software
cmurphy@modelworks.com
http://www.modelworks.com/express
-----From: "David W. Gillett"
The leading '_' in '_export' and '_declspec' indicates that these
are not standard parts of the language, but are compiler-specific.
So the short/rude answer is that Microsoft is free to ignore what
Borland does with '_export' when deciding what to do with
'_declspec'.
The longer answer is that I really doubt that you can meaningfully
export the template itself from the DLL, because that would require
users of the DLL to somehow be able to instantiate new template-based
classes relying on code hidden inside the DLL.
What you *can* reasonably expect to do is instantiate classes in
the DLL using the template, and export them. It seems to me that the
Borland approach lets you export all/whatever instantiations of the
template that happen to occur in your DLL -- this is convenient, but
may be overkill and questionably robust.
It looks to me like MS doesn't let you do this. But what I would
try instead -- and I would expect it to work for both compilers -- is
to explicitly instantiate the template-based classes you really want
to export, and export those on a case-by-case basis. (Or not, if
there are some instantiations that you want to be private to the
DLL.)
This is a little bit of work, but provides you with finer control
over what gets exported -- IMHO, you should already be explicitly
instantiating the classes you want exported, so the additional effort
is really trivial.
Dave
Terry Trippany -- terryt@str.com
Tuesday, April 23, 1996
Hi,
It is my belief that template classes cannot be exported using VC++ 4.0. In
order to export classes you must tag them with declspec(dllexport) in the
library, and _declspec(dllimport) in the using module. You can't do this in
the template definition, and thus, can't export the templatized class. I do
believe you can however export individual methods in a template class.
Terry Trippany
Strategic Technology Resources
"I persist, therefore I am!"
At 11:21 PM 4/17/96 -0600, you wrote:
>[Mini-digest: 2 responses]
>
>Yves Monier wrote:
>>
>> Hello,
>>
>> My environment is VC++ 4.0 / NT 3.51.
>>
>> I am trying to build a DLL (32 bits, of course). This DLL has to export many
>> classes, using the _declspec(dllexport) specifier.
>> Some of these classes are templates classes. So I thought that something like
>> the following code should be ok :
>>
>> template class _declspec(dllexport) Class1 {
>> // ...
>> };
>>
>> The problem is that the compiler produces two errors :
>> error C2960: template 'class' requires a tag-name
>> error C2988: unrecognizable template declaration/definition
>>
>> Of course, if I remove the _declspec(dllexport) specifier, no errors are
>> produced, but also no .lib file for my DLL ! :-(
>>
>> The same code compiled without any problem with Borland C++ 4.5 (to produce a
>> 16 bits DLL, with MYEXPORT defined to _export).
>>
>> Any idea ?
>>
>> Thanks in advance,
>>
>> Yves
>
>Yves,
>
>A template class is built by the compiler not by a linker using object
>code. If you want to export a template class you have to provide the
>C++ source code.
>
>--Chet Murphy
>ModelWorks Software
>cmurphy@modelworks.com
>http://www.modelworks.com/express
>
>-----From: "David W. Gillett"
>
> The leading '_' in '_export' and '_declspec' indicates that these
>are not standard parts of the language, but are compiler-specific.
>So the short/rude answer is that Microsoft is free to ignore what
>Borland does with '_export' when deciding what to do with
>'_declspec'.
>
> The longer answer is that I really doubt that you can meaningfully
>export the template itself from the DLL, because that would require
>users of the DLL to somehow be able to instantiate new template-based
>classes relying on code hidden inside the DLL.
> What you *can* reasonably expect to do is instantiate classes in
>the DLL using the template, and export them. It seems to me that the
>Borland approach lets you export all/whatever instantiations of the
>template that happen to occur in your DLL -- this is convenient, but
>may be overkill and questionably robust.
> It looks to me like MS doesn't let you do this. But what I would
>try instead -- and I would expect it to work for both compilers -- is
>to explicitly instantiate the template-based classes you really want
>to export, and export those on a case-by-case basis. (Or not, if
>there are some instantiations that you want to be private to the
>DLL.)
> This is a little bit of work, but provides you with finer control
>over what gets exported -- IMHO, you should already be explicitly
>instantiating the classes you want exported, so the additional effort
>is really trivial.
>
>Dave
>
>
>
Clarence Chiang -- clarence@spiderisland.com
Thursday, April 25, 1996
I think it is a more fundamental problem. Since a template class only
exists during compile-time, after the compiler instantiates a template
class with a concrete type the template class is gone. So it is no point
to export a template class in a dll since a program can't possibly
instantiates a template class with a concrete type at runtime.
Clarence Chiang
Spider Island Software
| Вернуться в корень Архива
|