   working with templates in a dll
  i have some strange problems with my template function.i wanted to generalize my function to test for a collision of 2 hash strings of different base strings, but it isn't working the way i want it to.i have my function declaration in the header file:__declspec(dllexport) bool TestCollision( unsigned long (*HashFunction)(char *) , unsigned long AnzCalcs = 4294967295);this is working well, and i can access it, but now i want to be able to use this one with other functions as well ( perhaps some, that return an int, __int64 etc... )this is now the new function declaration:template <class T> __declspec(dllexport) bool TestCollision( T (*HashFunction)(char *) , unsigned long AnzCalcs = 4294967295);but when i want to call the function with this codeint main(){lib::TestCollision<unsigned long>(lib::crc32);return 0;}i get an error ( in the app, that uses the function along with the dll, not in the dll project itself ): error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""bool __cdecl lib::TestCollision<unsigned long>(unsigned long (__cdecl*)(char *),unsigned long)" (??$TestCollision@K@lib@@YA_NP6AKPAD@ZK@Z)" in Funktion "_main".english: the linker can't find the definition in any source file for this function ( but it is defined like this: template <class T> bool TestCollision( T (*HashFunction)(char *) , unsigned long AnzCalcs) { ... } )   
  Where is the code for that specialization?If you haven't specializedbool TestCollision<unsigned long>( unsigned long (*HashFunction)(char *) , unsigned long AnzCalcs = 4294967295);...the code must exist somewhere. A function declaration is not enough.EG:// in a source file somewheretemplate<> // are you forgetting this?bool TestCollision<unsigned long>( unsigned long (*HashFunction)(char *) , unsigned long AnzCalcs){// implement here};  yeah, i got this code in the library project:template <class T> bool TestCollision( T (*HashFunction)(char *) , unsigned long AnzCalcs){T checkcrc = HashFunction(CheckString), crc = 0;// Some loop to test collision...return false; // No Collision found}my test project ( a simple console ) includes the header file with this definition:template <class T> __declspec(dllexport) bool TestCollision( T (*HashFunction)(char *) , unsigned long AnzCalcs = 4294967295);the funny thing is, that everything works fine when i don't use templates and limit my function to a return type of unsigned long ( or anythign else ) the linker just can't find my function when i use the template (when i compile my test project, the library compiles without any error or warning! )   ;  Uhhkg, I don't think you can expose templated functions from a compiled source (like a DLL). My reasoning for this is thus -The magic of templates work because the compiler is able to generate overloads at compile time. It only generates code for the calls it needs to - it doesn't do anything exhaustive. When you mark a template function as __dllexport, I'm pretty sure that either only the instances of the function created are exported, or even none at all.In the case of your above code, the compiler can't find the function because it doesn't exist. It has to be generated at compile-time from actual code.What this means is that you cannot export a generic template (in any case) with a DLL - you'll have to decide exactly what function instances you want and put them in.The solution to this, of course, is just to include your template in a header file which you include with the projects you want to use it in. This is how the STL, and pretty much every other library exposing template functionality works, because the raw code is built at compile-time - something that can't be accomplished with a DLL.Someone correct me if I'm wrong :/   ;Quote:Original post by MushuSomeone correct me if I'm wrong :/You're not.Σnigma   ;Quote:Original post by MushuUhhkg, I don't think you can expose templated functions from a compiled source (like a DLL). My reasoning for this is thus -The magic of templates work because the compiler is able to generate overloads at compile time. It only generates code for the calls it needs to - it doesn't do anything exhaustive. When you mark a template function as __dllexport, I'm pretty sure that either only the instances of the function created are exported, or even none at all.In the case of your above code, the compiler can't find the function because it doesn't exist. It has to be generated at compile-time from actual code.What this means is that you cannot export a generic template (in any case) with a DLL - you'll have to decide exactly what function instances you want and put them in.The solution to this, of course, is just to include your template in a header file which you include with the projects you want to use it in. This is how the STL, and pretty much every other library exposing template functionality works, because the raw code is built at compile-time - something that can't be accomplished with a DLL.Someone correct me if I'm wrong :/That's pretty much it... everything is instantiated at (none of the template functions exist in code until) compile time. The code for the template doesn't exist at all... so it can't be placed in a dll.   ;  *sniff* now i must think of another solution...i think i'll just leave this one out of the library...   ;Quote:Original post by SiS-Shadowman*sniff* now i must think of another solution...i think i'll just leave this one out of the library......or you can do like Mushu suggested... include another file after the header declaration... (something like an .ipp file?) and put the template code there.;  i've now put the code into a header file, wich is now suffering loneliness in my include folder ( muhaha ^^ )thanks for your quick help :)   ;  Just a quick clarification:What I meant by the code didn't exist until compile time, I was talking about the application project compile time... not the dll project compile time.When you compile the dll project, there would be no code for any of your specializations at all... because the template isn't instantiated at all. Therefore, there would be no code to put in the dll, and therefore "missing symbols" when you try to link the app.When you compile the app... that's when you're actually instantiating the template... but if there's no symbol to link, the linker will give you an error.Hope that clears it up somewhatChad