Click to See Complete Forum and Search --> : why dll project doesn't generate lib file?


reviver
January 16th, 2005, 09:52 AM
I use vs.net 2003 created a dll project (uncheck export symbols), when i wrote following code:
void __declspec(dllexport) test()
{
}
build the project will generate a export lib.
But when I wrote following code:
[In Math.h]
#ifdef MATHDLL_EXPORTS
#define _MATHCLASS __declspec(dllexport)
#else
#define _MATHCLASS __declspec(dllimport)
#endif

template <typename T>
class _MATHCLASS Math
{
public:
T Add(T x, T y);
};

[In Math.cpp]
template <typename T>
T Math<T>::Add(T x, T y)
{
return x + y;
}
Build the project only output a dll file, I have checked the setting as follows:
Linker->General: Ignore import library - NO
Linker->Advanced: Import Library: $(OutDir)/MathDLL.lib

Any one knows the problem? Or where i do wrong.
Thanks for your help, thx in advance!

reviver
January 16th, 2005, 10:02 AM
I have modifed the class, if i use a concrete class instead of a template class, it works, but i still don't know the sticking point, and if i want to define a template class in dll, how can i do.
thx for your help!

Mutilated1
January 20th, 2005, 04:44 PM
Because it is a template, there is no way to generate a lib because the compiler has no way of knowing what type you will fill out the template with later, thus there is no way to generate code that you can link to. But for your dll, you have specified a type so the only code that must be generated is comiled for the completed template type.

So in other words no compiler on earth can generate code for every possible completion of the template, so making a lib from a template doesn't really make sense in the first place. I know that can sound kind of backwards and confusing at first but its actually beautifull in what it means to you as a programmer. With templates if you don't use it, you don't have to pay the penalty for it by linking to it and only the code you will use will actually be compiled from the header file.

That makes it nice and lean because functionality in the template that you do not use will not be compiled and make your binary larger and as I just attempted to say, there is no way you can link to it.

reviver
January 21st, 2005, 07:57 PM
OH, thanks for your marvelous explain! I understand what you mean now.
Thanks!

Andreas Masur
January 22nd, 2005, 07:24 AM
Well...a common approach to get around this is to use the factory pattern...

Abstract Factory (http://home.earthlink.net/~huston2/dp/factory.html)
Factory Method (http://home.earthlink.net/~huston2/dp/factoryMethod.html)