我是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命令。
答案 0 :(得分:-1)
刚刚在load_scheduler定义之前添加了extern "c"
。 NVCC无法识别函数定义,因为它属于.cu
文件,因此错误。
extern "C"
void load_scheduler(int b, int n)
{
sched_func<<< b,n >>>();
}