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:

#include <stdlib.h>
#include <>

int main()
return 0;

#include <stdlib.h>

global void cuda_kernel()
int i = threadIdx.x;
i = i + 1;

void cudatest()
dim3 block(128,1,1);
dim3 grid(1,1,1);



when I compiled main.c with nvcc, errors came out:

In file included from main.c:2:
./ error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘void’
./ In function ‘cudatest’:
./ error: ‘dim3’ undeclared (first use in this function)
./ error: (Each undeclared identifier is reported only once
./ error: for each function it appears in.)
./ error: expected ‘;’ before ‘block’
./ error: expected ‘;’ before ‘grid’
./ error: ‘cuda_kernel’ undeclared (first use in this function)
./ error: expected expression before ‘<’ token

But when I rename main.c to without modifying anything else, I can compiled with nvcc successfully. Why?


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:


and 64bit:…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


Thank you for your help~