I'm creating a CUDA/C++ complex numbers/functions library. In particular, I have defined my own implementations of complex types int2_
, float2_
and double2_
in ComplexTypes.cuh
and ComplexTypes.cu
files and I have the following file structure:
Result_Types.cuh - Type traits file - Defines result_type_promotion
,
/*************/
/* PROMOTION */
/*************/
template <typename, typename> struct result_type_promotion;
....
template<> struct result_type_promotion< int2_, float2_ > { typedef float2_ strongest; };
....
/*************/
/* FUNCTIONS */
/*************/
template <typename> struct result_type_root_functions;
....
Operator_Overloads.cuh
template<typename T0, typename T1> __host__ __device__ typename result_type_promotion<T0, T1>::strongest operator+(T0, T1);
....
Operator_Overloads.cu - Overloads of common operations between complex numbers
#include "ComplexTypes.cuh"
#include "Result_Types.cuh"
#include "Operator_Overloads.cuh"
....
__host__ __device__ result_type_promotion<int2_,float2_>::strongest add(const int2_ a, const float2_ b) { result_type_promotion<int2_,float2_>::strongest c; c.c.x = a.c.x+b.c.x; c.c.y = a.c.y+b.c.y; return c; }
....
__host__ __device__ result_type_promotion<int2_,float2_>::strongest operator+(const int2_ a,const float2_ b) { return add(a,b); }
Function_Overloads.cuh
template<typename T0> typename result_type_root_functions<T0>::root_functions Asinh(T0);
Function_Overloads.cu - Overloads of functions on complex numbers
#include "ComplexTypes.cuh"
#include "Result_Types.cuh"
#include "Operator_Overloads.cuh"
__host__ __device__ result_type_root_functions<int>::root_functions Asinh(const int a) { return asinh((float)a); }
....
The above files, except for the type traits file which is dealt with as an include file, are compiled on the command line using nvcc
and cl
to form a .lib
file.
Unfortunately, when I compile the main function which includes
#include "ComplexTypes.cuh"
#include "Result_Types.cuh"
#include "Operator_Overloads.cuh"
#include "Function_Overloads.cuh"
I have linkage errors of the type
Error 247 error : Undefined reference to '_ZplIN2BB5int2_ENS0_7float2_EEN21result_type_promotionIT_T0_E9strongestES4_S5_' in '...Test.lib'
Please, note that:
- Only the complex types
int2_
,float2_
anddouble2_
are in aBB
namespace, but I'm addingusing namespace BB;
in all the definition files; - The problem arises when I'm using
+
in theFunction_Overloads.cu
file; if I'm not using+
, then no problem arises; - I (surprisingly) can use
+
in themain
function without receiving any linkage error.
Any idea to solve this problem?
Thanks.
EDIT
Following billz suggestions, I have solved the problem by adding explicit instantiations in the Function_Overloads.cu
file, like
__host__ __device__ result_type_promotion<int,int2_>::strongest operator+(const int a,const int2_ b);
....