Hi, everyone, I’m a cuda learner
I found that in all the samples of SDK main() functions are kept in .cu file. I want to know if main() could be in a .c file and just call a cuda function declared in .cu file or if .cu files could be compiled to a .o file which I can link when I compiled a standard C programme and how?
I have tested as follow:
//main.c
#include <stdlib.h>
#include <cuda.cu>
int main()
{
cudatest();
return 0;
}
global void cuda_kernel()
{
int i = threadIdx.x;
i = i + 1;
return;
}
void cudatest()
{
dim3 block(128,1,1);
dim3 grid(1,1,1);
cuda_kernel<<<block,grid>>>();
return;
}
when I compiled main.c with nvcc, errors came out:
In file included from main.c:2:
./cuda.cu:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘void’
./cuda.cu: In function ‘cudatest’:
./cuda.cu:13: error: ‘dim3’ undeclared (first use in this function)
./cuda.cu:13: error: (Each undeclared identifier is reported only once
./cuda.cu:13: error: for each function it appears in.)
./cuda.cu:13: error: expected ‘;’ before ‘block’
./cuda.cu:14: error: expected ‘;’ before ‘grid’
./cuda.cu:16: error: ‘cuda_kernel’ undeclared (first use in this function)
./cuda.cu:16: error: expected expression before ‘<’ token
But when I rename main.c to main.cu without modifying anything else, I can compiled main.cu with nvcc successfully. Why?
thanks!