对CUDA实现功能的未定义引用
Undefined reference to a function for cuda implementation
我写了一个main.cpp
,其中包含Common.h
文件
#include "Common.h"
int main(int argc , char **argv)
{
...
DeviceFunc(a_h , numvar , b_h); //Showing the data
....
}
然后,Common.h
包含:
#ifndef __Common_H
#define __Common_H
#endif
void DeviceFunc(float * , int , float *);
另外,DeviceFunc.cu
在同一文件夹中:
#include<cuda.h>
#include<stdio.h>
#include "Common.h"
__device__ __global__ void Kernel(float *, float * ,int );
void DeviceFunc(float *temp_h , int numvar , float *temp1_h)
{
float *a_d , *b_d;
//Memory allocation on the device
cudaMalloc(&a_d,sizeof(float)*(numvar)*(numvar+1));
cudaMalloc(&b_d,sizeof(float)*(numvar)*(numvar+1));
//Copying data to device from host
cudaMemcpy(a_d, temp_h, sizeof(float)*numvar*(numvar+1),cudaMemcpyHostToDevice);
//Defining size of Thread Block
dim3 dimBlock(numvar+1,numvar,1);
dim3 dimGrid(1,1,1);
//Kernel call
Kernel<<<dimGrid , dimBlock>>>(a_d , b_d , numvar);
//Coping data to host from device
cudaMemcpy(temp1_h,b_d,sizeof(float)*numvar*(numvar+1),cudaMemcpyDeviceToHost);
//Deallocating memory on the device
cudaFree(a_d);
cudaFree(b_d);
}
}
现在,当我用nvcc -o main main.cpp
编译代码时,我会得到此错误main.cpp:(.text+0x3a0): undefined reference to 'DeviceFunc(float*, int, float*)'
问题是什么
未定义的函数参考是在编译器找到函数的原型 and 时找不到链接>> >的原型。。为了避免此链接错误,您应该1)在一个命令处编译整个文件,或2)将编译和链接进程分开。我建议后者如下:
nvcc -c main.cpp
nvcc -c DeviceFunc.cu
nvcc -c Kernel.cu
nvcc main.o DeviceFunc.o Kernel.o -o main
请注意,您所显示的代码会错过包含车身Kernel
功能的文件。我认为Kernel
功能的主体包含在Kernel.cu
中。
相关文章:
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用