Linking CUDA and C files Undefined symbols


Well i’ve been reading in the forum trying to find an answer to my problem but I couldn’t so I need some of your help, I’m new to CUDA and I’m developing a project. Basically I have this 3 files:

#include "TestCuda.h"

__global__ void testCU(){

	int a;

	a = 10;


void function()





void function();


#include "TestCuda.h"

int main()




Now when compiling with this command:

nvcc -c Test.c

nvcc TestCuda.c -o test Test.o

I got these errors:

Undefined symbols:

  "_function", referenced from:

      _main in tmpxft_0000046b_00000000-4_Test.o

ld: symbol(s) not found

collect2: ld returned 1 exit status

The problem is that my MAIN function is in a C file and in the CU file I implement the function “function” which call the cuda-kernel “testCU()”

Any advise?

You need to declare function as [font=“Courier New”]extern “C” void function()[/font] and also change TestCuda.h. The latter is a bit trickier, since it is used both in C and C++ code. Conditional compilation helps here:

#ifdef __cplusplus

extern "C"


void function();

The reason is that C++ by default uses a different naming convention for functions, so that it can mangle the argument types into the name.

Thanks for the quick answer, finally it worked, I was declaring my function as extern “C” but I wasn’t modifying the .h file.

Just another question, I’m using OpenCV to read images but when I try to compile with nvcc it show an error about different architectures, my dynamic libraries of OpenCV are in “x86_64” and when I compile with nvcc it tries to create just an “i386” binary, so what is the best way to make it work in the linkin phase? I’m using these options in when compiling

-Xcompiler "-arch x86_64" --no-align-double

is there a way to build CUDA in x86_64 architecture? because I try to build OpenCV just for “i386” but I had no luck.

Thanks again.

OK, finally I could achieve it.

If someone needs to know how just compile with this line

nvcc *.c *.cu -o test -I/usr/local/include/opencv -L/usr/local/lib -lcxcore -lcv -lml -lhighgui -lcvaux -m64

Now another technical question is, what good/bad things can imply the fact of compiling with -m64? As far as I understand I’ll can run that application just in systems 64bit-compatible, but I’m not sure in this assumption.

Thanks again.

I tried this but I also tried another thing, in TestCuda.h I put the conditional compilation, but in I didn’t declare the function as extern “C”, and it compiles and executes very well, is this a bad practice of compilation? or is it as good as the compilation you proposed?