CUDA链接器错误与模板类 [英] CUDA linker error with template class

查看:183
本文介绍了CUDA链接器错误与模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ubuntu上使用CUDA 5.0和gcc / g ++ 4.6,当使用模板链接CUDA代码时,我遇到错误。

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:

cu_array.cu:

#include "cu_array.hpp"
template<class T>
CuArray<T>::CuArray(unsigned int n) {
  cudaMalloc(&data,n*sizeof(T));
}

cu_array.hpp:

cu_array.hpp:

#pragma once
template<class T>
class CuArray {
public:
   CuArray(unsigned int n);
private:
  T* data;
};

main.cu:

#include "cu_array.hpp"
int main() {
  CuArray<float> a(10);
}

使用 nvcc -c ,但是链接到 nvcc cu_array.o main.o 给出未定义的引用CuArray< float> :: CuArray(unsigned int) / code>。如果我将cu_array.cu的内容移动到头中,并且只构建main,它使用模板很好。或者,如果我删除模板,完全,代码自然链接罚款。

These compile fine with nvcc -c, but linking with nvcc cu_array.o main.o gives undefined reference to CuArray<float>::CuArray(unsigned int). If I move the contents of cu_array.cu into the header and only build the main, it uses the templates just fine. Or if I remove the templates altogether, the code naturally links fine.

我敢肯定有一个简单的答案。任何想法?

I'm sure there's a simple answer for this. Any ideas?

推荐答案

您尚未在定义的编译单元中实例化类,因此编译器不会发出类成员函数的任何代码,并且链接失败。这不是特定于CUDA,这种贪婪的实例化风格是g ++使用的编译/链接模型,很多人被它抓住了。

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.

否则,如果你显式实例化 CuArray :: cu_array.cu 底部的

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.

这篇关于CUDA链接器错误与模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆