【问题标题】:CUDA linking Error undefined reference [duplicate]CUDA链接错误未定义参考[重复]
【发布时间】:2015-11-07 01:38:31
【问题描述】:

我是 CUDA 编程新手,因此遇到了编译/链接文件的问题。我正在尝试编译 .c.cu 文件。 以下是文件:
p3.c:

#include <stdlib.h>
#include <stdio.h>  

extern void load_scheduler(int k, int j);
int blocks, threads;

int main(int argc, char* argv[])
{
    if (argc > 1)
    {
        blocks = atoi(argv[1]);
        threads = atoi(argv[2]);
    }
    else
        exit(1);

    load_scheduler(blocks, threads);

}

还有scheduler.cu文件:

#include <stdlib.h>
#include <stdio.h>

__global__ void sched_func()
{   
    int j = 6*5*threadIdx.x;
    printf("%d\n",j);
}

void load_scheduler(int b, int n)
{
    sched_func<<< b,n >>>();
}

我使用 nvcc -c scheduler.cu p3.c 编译这两个文件,看起来不错
但是,当我尝试使用 nvcc -o cuda_proj scheduler.o p3.o 链接这两个文件时,出现错误:

p3.o: In function `main':
p3.c:(.text+0x58): undefined reference to `load_scheduler'
collect2: ld returned 1 exit status

我可能没有使用正确的步骤来完成这项工作,所以如果我应该尝试任何其他方法,欢迎提出建议。我也是制作 Makefile 的新手,所以想坚持在终端上使用 nvcc 命令。

【问题讨论】:

    标签: c compilation cuda nvidia


    【解决方案1】:

    刚刚添加:extern "c" 在 load_scheduler 定义之前。 NVCC 无法识别函数定义,因为它属于.cu 文件,因此出现错误。

    extern "C"
    void load_scheduler(int b, int n)
    {
        sched_func<<< b,n >>>();
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 2020-10-21
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多