Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ CUDA从单独的文件调用设备功能(名称混乱?)_C++_Cuda - Fatal编程技术网

C++ CUDA从单独的文件调用设备功能(名称混乱?)

C++ CUDA从单独的文件调用设备功能(名称混乱?),c++,cuda,C++,Cuda,我应该如何正确地做到这一点?以下是代码的简化: //main.cu #include "math.cuh" __global__ void test(float *x, unsigned numElements) { int i = blockDim.x * blockIdx.x + threadIdx.x; if (i < numElements) { float array[5] = {1, 2, 3, 4, 5};

我应该如何正确地做到这一点?以下是代码的简化:

//main.cu    
#include "math.cuh"

__global__ void test(float *x, unsigned numElements)
{
    int i = blockDim.x * blockIdx.x + threadIdx.x;

    if (i < numElements)
    {
        float array[5] = {1, 2, 3, 4, 5};
        copyArray(x + 5*i, array, 5);
    }
}

int main(int argc, char **argv)
{
    test<<<blocksPerGrid, threadsPerBlock>>>(d_A, numElements);
}

//math.cuh
__device__ void copyArray(float *dest, float *src, unsigned length);

//math.cu
#include "math.cuh"
__device__ void copyArray(float *dest, float *src, size_t length)
{
    for (int i = 0; i < length; i++) {
        dest[i] = src[i];
    }
}
得到了这个错误:

nvlink error   : Undefined reference to '_Z9copyArrayPfS_j' in '/tmp/tmpxft_00000265_00000000-21_main.o'

这显然看起来像是一个名称损坏错误,但我尝试在各种地方放置extern“C”,但它不起作用。

函数原型使用
无符号
,而定义使用
大小
。这就是原因吗?

什么操作系统?您使用的是make还是cmake?请进一步澄清问题,链接时出现了什么错误,何时发生的?您到底做了什么来调用该函数?这似乎是一个单独的编译问题。尝试使用
-rdc=true
编译。在这方面有很多问题需要单独编译,例如:。同意@jackolanten。如果函数在
main.cu
中调用,在
file.cuh
中声明,并在
file.cu
中定义,则肯定需要单独的编译/设备代码链接。杰克,你想提供一个答案吗?我会投票的。或者让我们标记为重复。请尝试
nvcc-arch=sm_20-o main-rdc=true main.cu file.cu
问题标题似乎很可能会出现在搜索中,因此这里的答案将非常有用。
nvlink error   : Undefined reference to '_Z9copyArrayPfS_j' in '/tmp/tmpxft_00000265_00000000-21_main.o'