开发者

How to compile OpenCV code using a Cuda shared library compiled using nvcc?

开发者 https://www.devze.com 2023-03-23 17:38 出处:网络
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.

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 comp开发者_如何学JAVAile 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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号