Link to home
Start Free TrialLog in
Avatar of Lescha
Lescha

asked on

Import a template class from DLL

I have a template class. As long as it was part of a program, everything worked fine. However, now I want to put it into a DLL, export this class and import it into my program.

I created the DLL and exported the class. Then, I copied the LstMgr.h, LstMgr.lib and LstMgr.dll files to the program which will use the class. Then, in the main program I wrote something like:

#include "LstMgr.h"
...
typedef ListManager<int> IntList;
...
void main(void)
{
  IntList l1;
  IntList *l2;
  l2 = new IntList;
}

This doesn't work! The compilation goes fine, but the linker says it cannot find the constructor of the class.

Is there some problem with ex/importing a template class?
Avatar of BSoeters
BSoeters

Taken from MSDN October 1999, article:
"DLLs for Beginners", Debabrata Sarma, Microsoft Developer Support, November 1996

...
Exporting Template Class and Functions

Visual C++ does not support export of template class or functions. One reason for this arises from the notion of a template itself. A class template is an abstract representation of similar, but distinct, types. There is no code or data that exists to be exported until an instance of a class template is generated. One workaround (not recommended) is to declare the template class or functions with the declspec attribute in the DLL and instantiate all possible types in the DLL source. As you can see, this defeats the very purpose of using templates.

For the same reason, you cannot export the Standard Template Library (STL) classes or functions.
...
Avatar of DanRollins
Typically, one writes a series of access functions and exports those.

-- Dan
Avatar of Lescha

ASKER

So, what should I do? And how does STL work - by supplying the code to the programmers?

Dan, I didn't quite understand your comment. How can I write access functions for every member function of the class? And how will it help, anyway?
>>How can I write access functions for every member function of the class?

That is not usually necessary.  All you typically need to export is a CreateList, DeleteList, AddItem, GetItem, and perhaps ClearAllItems.

Yes, this appears to invalidate the whole point of using a STL class.  But given the clearly-documented fact that STL cannot be used in a DLL that way, what else is there to do?

>>And how will it help, anyway?
By doing that, the calling EXE is requesting the called DLL to do the work.  That lets the data itself remain as part of the DLL.  Without the access functions, the EXE will attempt to use its own (invalid) copy of the data.

-- Dan
You cannot export a template class from a DLL.But instead
you cannot go for a static linking because the linker start
giving the problems.

 the linker cannot get the definiton of  symbol generated by the compiler when the type is specified in the program using the libarary because the function has a different symbol existing in the library.

//Writing series of access functions.

write a set of functions(non-template functions which can be exported and use them.

example:typedef ListManager<int> IntList;
...
void main(void)
{
 IntList l1;
 IntList *l2;
 l2 = new IntList;
}

//So, what should I do? And how does STL work - by supplying the code to the programmers?

In the template library there is no meaning to supply or
not to supply the source code because there is no specific
type information in the code.

I will work on this and get back to you with a proper code.


Avatar of Lescha

ASKER

Dan:

These access functions accept arguments of a pre-specified type, right? They cannot be template-functions, can they?

And in that case, what do I win? Instead, I can declare a specific class in the DLL, like IntList, and export it...
>> in that case, what do I win?

That is correct.  There is no win here.

A template class looks like a regular class, but it is not.  It is really more like a complex set of #define macros.  The *compiler* expands its code inline in your program.  

I have never understood why so many C++ programmers are so emamoured with STL.  If you need an array of ints, just make one.  If you need an array of objects of type CFoo, then make one.  It's not like you are reusing code or something.  Using templates is just a convoluted way to hide what is really going on in your program.  Have you ever tried to single-step though a call into an STL object member function?  Ugggg.

-- Dan
Avatar of Lescha

ASKER

Here is an example of what you win: suppose you have a Linked-List Manager class which manages abstract data, and suppose you want to clusterize your screen.

If, which is unlikely, you are unfamiliar with the term, here is what it means: having an array of points, divide it into subarrays using the relative closeness criterion. That is, determine which points are "close" to the first, which are "close" to those "close" to the first, etc. These form the first cluster. Then, you take the first point not belonging to the first cluster and start doing the same with it and the rest of your array.

The result is a maximum of N arrays with a maximum of N points in each array (there is more maths to it, but this is the worst case). The most logical way to store such a monstrosity is to transfer points from the original array into linked lists: one per cluster; then you need to store all clusters somewhere. Thus, you have a linked list whos data is linked list whos data is point.

This way, you save search times, merge times, etc. In short, managing times. Of course, it's not the only way, but it's the fastest.

There are other examples, of course, but what I mean by all this is simple: there are uses for abstract-data (or template) classes.
Avatar of Lescha

ASKER

Suppose in my DLL I write something like:

typedef ListManager<int> IntList;

Is there a way I can export IntList out of DLL, for use in the main program? How do I do that?
Have you seen:

HOWTO: Exporting STL Components Inside & Outside of a Class (Q168958)
http://support.microsoft.com/support/kb/articles/Q168/9/58.ASP

The relevant phrase is about halfway down the page:
"
The only STL container that can currently be exported is vector.
"

This discusses some of the "watch-out-fers"
PRB: Access Violation When Accessing STL Object in DLL (Q172396)
http://support.microsoft.com/support/kb/articles/Q172/3/96.ASP

=-=-=-=-
>>there are uses for abstract-data (or template) classes.

Lescha,
Your last post basically made *my* point.  You don't need abstract data.  You have two concrete data types.  Point and Cluster.   What you have is a standard hierarchy, similar to a tree-structured directory system where clusters are equivalent to directories and points are equivalent to files.  

A single, simple container would do nicely.  Each data element has a flag to indicate if it is a cluster or a point.  You write a few simple functions to traverse the tree from an arbitrary location and a way to enumerate leaf notes of any branch.  Simpler is better.

-- Dan
Avatar of Lescha

ASKER

Dan,

thanks for the references. The only thing left here that I don't know is my last question (repeated here):

typedef ListManager<int> IntList;

How do I export from the DLL something like this?


<By the way, I have increased the points for this question, since it turned out into a real discussion...>
>>The only thing left here that I don't know is my last question

You can keep repeating the question if you want, but the answer will always remain the same.   You can't -- or more precisely, if you do it will appear to work, but will fail with GPFs, etc.

"
The only STL container that can currently be exported is vector.
"

-- Dan
Avatar of Lescha

ASKER

I didn't mean to _repeat_ the question, I thought I might be able to fool C++ by explicitly stating that what I want to export is LLManager of Integers...

So, what I need is define N classes, which will have exactly same methods, but each will deal with its type of data?!!
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial