There is no such thing as a “template function”. However, there is a function template; This is the template from which functions are created by instantiating. In this case, distinction is important.
To call a function created from a template, you must have access to this instance. The most common case is to implement a template in the header file and just #include it (see this SO question for more details). I believe that you want your function to be usable with arbitrary client types like LinearOperation and Vector , so the only option is for headers only.
If, on the other hand, you know all the types that you want to instantiate the template when creating the library, you can explicitly create an instance of the template for these types and export these explicit instances. Like this:
Header file
template <typename LinearOperator,typename Vector> void cuspDsolver(LinearOperator& A,Vector& X,Vector& B,double tol);
Original file
template <typename LinearOperator,typename Vector> void cuspDsolver(LinearOperator& A,Vector& X,Vector& B,double tol) {
Such instances cannot be extern "C" , however (they all have the same function name, after all). Therefore, if you want to dynamically load them, you will have to give them access to them with unique names.
However, I believe that you are really looking for implementing funciton in the header file.
Based on your comments, here's how you could make your library dynamically load when using CUSP internally.
You cannot have a function template in the public library interface. Therefore, suppose you want to allow your library to be used with the following LinearOperator types: OperatorCharm and OperatorTop and with the following Vector types: FancyVector<float> and FancyVector<double> . Then your open interface might look like this:
template <typename LinearOperator,typename Vector> void cuspDsolver(LinearOperator& A,Vector& X,Vector& B,double tol) {
You don’t even need to explicitly create an instance of the template, since it will be implicitly created for calls in EXPORT -ed functions.
Thus, your public API will be those 4 cuspDsolver_a_b functions that can be requested dynamically as usual.