CUDA and C++ Do i need a main function in the .cu?

Hi, i have a .cu file with a kernel and a function that calls that kernel (and do the transfer between memories stuff).

I compile it by /usr/local/cuda/bin/nvcc and the other files in the project using g++.

when i


i get the error:

/usr/lib/gcc/i486-linux-gnu/4.3.2/…/…/…/…/lib/crt1.o: In function `_start’:

(.text+0x18): undefined reference to `main’

I just want to call the function from a class. Do i need a main function in the .cu file or something?

Anyone knows what am i doing wrong?

If you need some code, just ask for it.

I’ll post my makefile, just in case.

# Proyect: Paralel Reinforcement Evolutionary Artificial Neural Network

preann : XMM32.o paralelLayer.o AbstractLayer.o CudaLayer.o XmmLayer.o CppLayer.o main.o 

	g++ -o preann XMM32.o AbstractLayer.o CudaLayer.o XmmLayer.o CppLayer.o main.o paralelLayer.o

main.o : main.cpp NeuralNetwork.o

	g++ -c main.cpp

NeuralNetwork.o : NeuralNetwork.cpp NeuralNetwork.h CppLayer.o XmmLayer.o CudaLayer.o

	g++ -c NeuralNetwork.cpp

CppLayer.o : CppLayer.cpp CppLayer.h AbstractLayer.o

	g++ -c CppLayer.cpp

XmmLayer.o : XmmLayer.cpp XmmLayer.h AbstractLayer.o XMM32.h XMM32.o

	g++ -c XmmLayer.cpp

CudaLayer.o : CudaLayer.cpp CudaLayer.h AbstractLayer.o paralelLayer.o

	g++ -c CudaLayer.cpp

AbstractLayer.o : AbstractLayer.h

	g++ -c AbstractLayer.h

paralelLayer.o :


XMM32.o : XMM32.asm

	nasm -f elf XMM32.asm


	rm preann XMM32.o AbstractLayer.o CudaLayer.o XmmLayer.o CppLayer.o main.o paralelLayer.o


The command


will cause nvcc to produce an executable, just like gcc. You need to let nvcc know you want to produce an object file (just like gcc):

/usr/local/cuda/bin/nvcc -c

Thanks a lot!

i was stuck in that…

I’m in front of the same problem !
I get the answer (thanks for it) but I did not succeed dealing with two compilers !
For example, I have a source code A (sourceA.cpp) containing a main function and a source code B ( containing only a function.
In the main of A, I called the function in B. I’m compiling the with nvcc -c. But after that, how can I “explain” to gcc that the function it’s looking for has been already compiled ?
Maybe my question is really easy but I can’t find the way of doing it.

Sorry, i was doing other things. I still didn’t try to call the function, so i’m not sure if what i’m going to tell you is right.

I think you must write in your C++ code something like:

extern "C" void yourCudaFunction ();

I did it to use assembly functions from C++ and it was enought.

Only one function?

I think you can’t call a kernel directly from C++. You need to write a “normal” function in the .cu.

In the examples, that function calls the kernel and execute all that cudaMalloc and cudaMemCpy stuff.

i hope one of this two things can help you.