由于缺少类模板,NVCC 编译面向对象代码时出现问题
nvcc problems compiling object oriented code due to missing class template
我在使用nvcc
编译 CUDA 代码时遇到问题。为了演示它,我创建了一个虚拟类来表示 3D 空间中的表面。
文件surface.h
如下:
#ifndef SURFACE_H
#define SURFACE_H
class surface
{
private:
float dX; // grid resolution in x [m]
float dY; // grid resolution in y [m]
int nX; // number of elements in x
int nY; // number of elements in y
float* depth; // pointer to depth array [m]
public:
__host__ __device__ void set_dim(const int _nX, const int _nY);
__host__ __device__ void set_res(const float _dX, const float _dY);
__host__ __device__ float get_surface_mm(const int iX, const int iY);
};
#endif
这是相应的surface.cpp
文件:
#include "surface.h"
__host__ __device__ void surface::set_dim(const int _nX, const int _nY){
nX = _nX;
nY = _nY;
return;
}
__host__ __device__ void surface::set_res(const float _dX, const float _dY){
dX = _dX;
dY = _dY;
return;
}
__host__ __device__ float surface::get_surface_mm(const int iX, const int iY){
float surfLvl = (float) iX * iY;
return surfLvl;
}
我正在尝试用nvcc -x cu -arch=sm_50 -I. -dc surface.cpp -o surface.o
编译它,但出现以下错误:
surface.h(4): error: argument list for class template "surface" is missing
surface.cpp(7): error: argument list for class template "surface" is missing
surface.cpp(8): error: identifier "nX" is undefined
surface.cpp(9): error: identifier "nY" is undefined
surface.cpp(13): error: argument list for class template "surface" is missing
surface.cpp(14): error: identifier "dX" is undefined
surface.cpp(15): error: identifier "dY" is undefined
surface.cpp(19): error: argument list for class template "surface" is missing
8 errors detected in the compilation of "/tmp/tmpxft_000bedf2_00000000-6_surface.cpp1.ii".
我真的不明白这个错误的原因,因为在我看来,该类是完全定义的,编译器应该知道参数列表。你们中是否有人已经遇到过类似的问题?如果我删除__device__
和__host__
标志并用gcc
编译它,一切正常。
nvcc --version
输出:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Wed_Oct_23_19:24:38_PDT_2019
Cuda compilation tools, release 10.2, V10.2.89
我知道 CUDA 不一定支持面向对象编程的每个功能,但仔细检查我在这里尝试编译的内容是否应该兼容。
我很欣赏:)的每一个提示。提前非常感谢。
此代码的唯一问题是surface
已经是导致此问题的内置cuda.h
类型。重命名后,一切都运行而没有错误。
相关文章:
- 分段错误(内核转储) C++面向对象编程
- 由于缺少类模板,NVCC 编译面向对象代码时出现问题
- 在C++面向对象程序中,谁负责删除传递给构造函数的对象
- 是否有一种设计模式或面向对象的基本原则来处理这种共享资源的情况?
- 对"车辆的 vtable"的未定义引用 - 面向对象的编程练习
- 关于 C++ 中面向对象中的 decltype 类型
- 多态性的面向对象方法
- 成员功能在面向对象的框架中返回std :: vector.size()的效率
- 以面向对象的方式解析自定义数据包
- 面向对象编程 - 位域私有结构的公共参考
- 是否可以将匿名 lambda 函数绑定到对象以允许 lambda 中的代码访问对象的成员?
- 面向对象的编程-C 练习
- 如何使用面向对象的技术在 win32 和 c++ 中创建编辑控件
- C++面向对象的字典程序
- 面向对象架构,用于简单的基于着色器的GL程序
- 面向对象 成本计算C++不起作用
- 如何使我的 Windows API GUI 代码更加面向对象
- 理解面向对象代码的工具和方法
- 带超时的多线程面向对象套接字代码
- 在保持良好的面向对象设计的同时重用代码以提高性能的最佳方法是什么?