I’ve a file A.cu containing function void A(). I’ve another file test_A.c which calls the cuda function A() and also has function declaration as
extern void A();
Now I compile and link them as follows
nvcc -c -o A.o A.cu
gcc -o test_A test_A.c A.o /opt/cuda-4.0/cuda/lib64/libcudart.so
and I get error like
undefined reference to `A'
What am I missing?
The problem is probably C++ name mangling. CUDA code is compiled with a C++ compiler, not a C compiler. If you dump the contents of
A.oyou should find a symbol called something like_Z1Av, rather thanAas the C compiler is expecting.To overcome this, you can use the following declaration inside your
A.cufile:This will tell the C++ compiler to use C conventions when compiling the code, and a suitably named function will be written to the object file
A.owhich the C compiler and linker can understand.