1
votes

For a test I have written a code of matrix multiplication in C(cuda) and compiled it using nvcc to create shared library using following command.

nvcc -c MatMul.cu -o libmatmul.so

Then i wrote a OpenCV code in C and tried to compile with following command.

gcc ImgMul.c `pkg-config --cflags --libs opencv` -L. -L/usr/local/cuda/lib64 -I/usr/local/cuda/include -I. -lmatmul -lcudart -o ImgMul

and I am getting following error.

gputest.c:(.text+0x3f): undefined reference to `matmul'

Could anyone tell me how to include cuda libraries while compiling a code in gcc.

OS: Ubuntu gcc : 4.4.0

1

1 Answers

1
votes

The first point to make is that

nvcc -c MatMul.cu -o libmatmul.so

does not make a shared library, it just compiles to an object file. Shared libraries and object files are not at all the same thing.

That aside, the reason for the symbol not found error is C++ name mangling. Host code in CUDA source files is compiled using the host C++ compiler, not C. So symbol names in the host code emitted by the compiler are subject to name mangling. To get around this, the easiest way is to declare functions which you wish to call from plain C code using the extern "C" declarator (see here for a reasonable overview of the perils of C/C++ interoperability).