CUDA linker error with template class

前端 未结 1 533
深忆病人
深忆病人 2021-01-06 04:15

Using CUDA 5.0 on ubuntu with gcc/g++ 4.6, I\'m getting errors when linking against CUDA code with templates.

cu_array.cu:

#include \"cu_array.hpp\"
         


        
相关标签:
1条回答
  • 2021-01-06 04:49

    You haven't instantiated the class in the compilation unit where it is defined, so the compiler doesn't emit any code for the class member function, and linkage fails. This isn't specific to CUDA, this greedy style of instantiation is the compilation/linkage model g++ uses, and lots of people get caught out by it.

    As you have found already, the simplest solution is to include everything into the same compilation unit, and the problem disappears.

    Otherwise if you explicitly instantiate CuArray::CuArray at the bottom of cu_array.cu like this:

    template CuArray<float>::CuArray(unsigned int);
    

    the compiler will emit code where it would otherwise not, and the linkage problem will be fixed. You will need to instantiate every class function for every type you want to use elsewhere in the code to make this approach work.

    0 讨论(0)
提交回复
热议问题