Let's say I have the following template function:
Template <typename T> void Fun(T t);
I'd like to be able to implement some specializations of Fun in a first DLL (DLL_1), some other specializations in a second (DLL_2), and still some other in a third (DLL_3), but DLL_2 and DLL_3 may make use of specializations implemented in DLL_1. So in DLL_2, Fun<T> may be both imported and exported, depending on T.
I somehow managed to implement it by always declaring (in the .h file) the template as dllimport:
template <typename T> __declspec(dllimport) void Fun(T t);
and always implementing the specializations as dllexport (relying on the fact that dllexport takes the precedence):
template <> __declspec(dllexport) void Fun(MyClass t)
{
...
}
but I get linker warnings:
warning LNK4217: locally defined symbol [...] imported in function [...]
and
warning LNK4049: locally defined symbol [...] imported
Is there a better way to do
Chris.

dllexport and function templates specializations
Vb Fan
Use macros in your function definitions that will resolve to dllimport or dllexport depending on which dll is currently being defined.
I have had similar problems with some STL classes and here's how I got around it.
-----------------------------------------------------------------
//CORE_UTIL_DLL is defined in the properties of the core_util project
//(c/c++ -> Preprocessor -> Preprocessor Definitions) and is undefined in
//all other projects
#if(defined CORE_UTIL_DLL)
// We are compiling up CORE_UTIL.DLL
#define CORE_UTIL_INST template class __declspec( dllexport )
#else
// we are compiling a different DLL/EXE
#define CORE_UTIL_INST template class __declspec( dllimport )
#endif
CORE_UTIL_INST vector<int>;
-------------------------------------------------------------------
So what that will do is instantiate the vector<int> specialisation in the core_util dll and just consume/import it in any other DLL that uses that header file.
Thats for a template class rather than a function but the principle should be the same.
I hope that makes sense
Regards
Jero
jagdishsingh
So its not necessary that all specializations are exported by one DLL.
RamaKrishna
imported means: resides in an external DLL
exported means: has an implementation in this DLL and exports its interface to other executables.
If the template is exported than it can be used inside the DLL without additional action. So when building the DLL dllexport must be used. When used from other modules dllimport must be used.
Chad Moran
CyberYodaCY
Chris.
FransM
The important point is: 'in DLL_2, Fun<T> may be both imported AND exported depending on T' or, better said, 'in DLL_2, Fun<T> may be either imported or exported depending on T'.
It would be possible to do as you suggest if I did not declare Func<T> as a function template but just as an exhaustive list of overloads (which I will probably end up doing).
Chris.