473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

templates exported from a DLL and /CLR

Hi All!

In native mode I can export class template from a DLL this way:
template <typename T> class TSuperType
{
public:
T doSomething();
};

....

template class __declspec (dllexport) TSuperType<int>;

And I can import it this way in another module:
template class __declspec (dllimport) TSuperType<int>;

But when I enable CLR support on both modules and recompile I get LNK2028
and LNK2019 errors about missing doSomething(). I don't have any explicit
calling convention anywhere and _both_ modules are compiled with /CLR. What
can I do?

TestCLRXportTemplates.obj : error LNK2028: unresolved token (0A00001C)
"public: int __thiscall TSupertType<int>::getData(void)"
(?getData@?$TSupertType@H@@$$FQAEHXZ) referenced in function "int __cdecl
wmain(int,wchar_t * * const)" (?wmain@@$$HYAHHQAPA_W@Z)

TestCLRXportTemplates.obj : error LNK2019: unresolved external symbol
"public: int __thiscall TSupertType<int>::getData(void)"
(?getData@?$TSupertType@H@@$$FQAEHXZ) referenced in function "int __cdecl
wmain(int,wchar_t * * const)" (?wmain@@$$HYAHHQAPA_W@Z)

C:\Documents\Visual Studio
2005\Projects\Test\TestCLRXportTemplates\Debug\Tes tCLRXportTemplates.exe :
fatal error LNK1120: 2 unresolved


Apr 16 '06 #1
5 4169
> In native mode I can export class template from a DLL this way:
template <typename T> class TSuperType
{
public:
T doSomething();
};

...

template class __declspec (dllexport) TSuperType<int>;
this will generate a compiler warning (C4661) with VS2005 because there is
no suitable definition for doSomething. i.e. there is no implementation of
that function, only a declaration. I assume that you implement that function
where you typed '...'?
I.e.
template <typename T> T TSuperType<T>::doSomething()
{
return T(); //or do something useful here
}
template class __declspec (dllexport) TSuperType<int>;
And I can import it this way in another module:
template class __declspec (dllimport) TSuperType<int>;


did you add the lib file of your dll to the linker input of your
application? otherwise the linker will not find the implementation of the
methods in your template and you will get those errors when building the
application.
Once the linker input is OK, and with all the methods defined before the
dllexport, the code compiles and links without a problem with /clr set.

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"

Apr 16 '06 #2

Yes, I implemented doSomething() to "return T();" just before
__declspec(dllexport) directive.

#include "templ.h"
template <typename T>
T TSuperType<T>::doSomething()
{
return T();
}
template class __declspec (dllexport) TSuperType<int>;

I have both projects in one solution and I just set dependencies between
them.
It compiles, links and works perfectly without /clr switch.
But it complains with /clr switch.

"Bruno van Dooren" <br**********************@hotmail.com> wrote in message
news:Ot**************@TK2MSFTNGP04.phx.gbl...
In native mode I can export class template from a DLL this way:
template <typename T> class TSuperType
{
public:
T doSomething();
};

...

template class __declspec (dllexport) TSuperType<int>;


this will generate a compiler warning (C4661) with VS2005 because there is
no suitable definition for doSomething. i.e. there is no implementation of
that function, only a declaration. I assume that you implement that
function where you typed '...'?
I.e.
template <typename T> T TSuperType<T>::doSomething()
{
return T(); //or do something useful here
}
template class __declspec (dllexport) TSuperType<int>;
And I can import it this way in another module:
template class __declspec (dllimport) TSuperType<int>;


did you add the lib file of your dll to the linker input of your
application? otherwise the linker will not find the implementation of the
methods in your template and you will get those errors when building the
application.
Once the linker input is OK, and with all the methods defined before the
dllexport, the code compiles and links without a problem with /clr set.

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"

Apr 16 '06 #3
> I have both projects in one solution and I just set dependencies between
them.
It compiles, links and works perfectly without /clr switch.
But it complains with /clr switch.


I did the same, but I also had to explicitly specify the lib file of the dll
as an additional linker input.
Doing that makes it work. not doing that results in the linker errors you
posted.
To be honest I don't know if it is a bug or a feature...

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 16 '06 #4
Thank you for you help.
I think it's a bug.
http://lab.msdn.microsoft.com/Produc...1-e533c026cc3f
"Bruno van Dooren" <br**********************@hotmail.com> wrote in message
news:Or**************@TK2MSFTNGP03.phx.gbl...
I have both projects in one solution and I just set dependencies between
them.
It compiles, links and works perfectly without /clr switch.
But it complains with /clr switch.


I did the same, but I also had to explicitly specify the lib file of the
dll as an additional linker input.
Doing that makes it work. not doing that results in the linker errors you
posted.
To be honest I don't know if it is a bug or a feature...

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"

Apr 16 '06 #5
> Thank you for you help.
I think it's a bug.
http://lab.msdn.microsoft.com/Produc...1-e533c026cc3f


I did some additional testing, and I think I know why it is like this.

If your dll is unmanaged, it is reasonable to expect that it would contain
exports.
if another project depends upon it, it is therefore reasonable to feed the
lib file into the linker command for the application. That is why 'Link
library dependencies' is set by default.

if you specify the /clr switch for the dll, the assumption that it will
contain unmanaged exports is no longer valid.
it could just as well be a classlib. in that case (no native exports) there
will be no lib file because there are no import directives to generate. If
there is no lib file, the build of your app would fail since it would expect
it to be there.

So on one hand, it is not unreasonable that the lib file is not
automatically fed into the linker command of the app (since the dependency
could be a managed one instead of an unmanaged one). On the other hand, the
flag 'Link library dependencies' is still set to yes, so you would expect it
to do so, regardless of the build options of the dll.
It seems that VS ignores the 'Link library dependencies' option for mixed
mode dlls

If that is so they should at least have mentioned it in the documentation
for 'Link library dependencies'
Anyway I have validated it, but only rated it as an annoyance because VS
errs on the safe side and it is trivial to work around

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: WebRod | last post by:
Hi, i want to rewrite my website to separate the php script from the HTML code. Therefore a web designer can update the design of the website without any knowledge in PHP. I read a lot of...
1
by: Marc André Ueberall | last post by:
Hi there! I've got the following problem and would be very happy if there is somebody with a solution! =) Btw. I'm using Microsoft Visual C++ 6.0 Std Edition I've generated a template class...
1
by: Bolin | last post by:
I was wondering how to properly export template classes that are defined in a library. The problem is that this library defines a template class: template <typename T> class A {...}; and...
1
by: Leslaw Bieniasz | last post by:
Cracow, 15.09.2004 Hi, I am writing a big C++ project using BCB 4.0. The project consists of several dlls and exes. Each of the dll is composed of several units (that is cpp files with...
5
by: Felix I. Wyss | last post by:
Good Afternoon, I recently noticed that some very simple methods of a template declared and used in a DLL library get inlined when used by the DLL itself, but not by other DLLs and EXEs. After...
7
by: Vyacheslav Lanovets | last post by:
Hello, All! One of our target platforms has only 32 MB of virtual memory (Windows CE), so we decided to explicitly load some of our dlls. But the classes created inside such dlls are created...
1
by: Terry Olsen | last post by:
I exported a Windows Service template from Visual Basic 2005. How can I import this so that VB Express will see it? I tried copying it to the User Templates folder but it doesn't see it. Does VB...
18
by: ciccio | last post by:
Hi all, The intel c++ compiler supports exported templates but I was wondering a bit about their used syntax. If I compare the syntax written in strostroup's book then there is a difference. ...
1
by: Rahul | last post by:
Hi Everyone, I was looking at the link, http://www.comeaucomputing.com/4.0/docs/userman/export.html and it looks like the basic purpose of exporting templates is make sure that they are...
26
by: puzzlecracker | last post by:
Team, C++ has been around since 1986, why templates are still regarded is a new feature by most compiler vendors and not fully supported (for example export feature). Look at other popular...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.