main() must be in .cu file?

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;
}

//cuda.cu
#include <stdlib.h>

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!

Hi Alex.

Yes the main() function could be in an other file, perhaps you should take a look at the examples supplied by nvidia, in the sourcecode you can se how the CUDA files can be compiled seperately and how the programs are combined at linktime

check the examples here:

32bit:
http://www.nvidia.com/content/cudazone/cuda_sdk/samples.html

and 64bit:
http://www.nvidia.com/content/cudazone/cud…amples_x64.html

A good example would be the sample Post-Process in OpenGL, in this exaple the OpenGl code is seperate from the cuda code and in c++ files with signatures for the cuda functions defined in the c++ files and the implementation in .cu files.

hope that helps

Martin

Thank you for your help~