Problem linking against custom CUDA shared library

Hi,

I have just finished porting a C library to CUDA and I’m trying to link my programs against the CUDA version.

My code is in the following layout: several “.cu” files contain kernels and the C “wrappers” that call the kernels.

The idea is to simply be able to swap out the existing C library with the CUDA one, without the existing programs (written in C) being any wiser about which library is actually doing the computations.

I created the shared library as follows (after already compiling each “.cu” file with nvcc -c):

nvcc -deviceemu -I../include --shared --compiler-options '-fPIC' *.o -o cudalib.so

But when I try to link any of my programs I get errors saying that there are undefined references to the functions it is trying to call.

I know that these functions are there, because they are right next to their respective kernels in the “.cu” files!

Any help would be much appreciated.

I have also tried adding ‘extern “C”’ before the function declarations and prototypes as suggested on other webpages, but the problem still persists :(

Well, after much trying, I am still having trouble getting this to work.

I read another thread about calling wrappers in ‘.cu’ files from a C program, here: http://forums.nvidia.com/lofiversion/index.php?t62601.html

After that, I tried compiling my project by including ALL the objects into the program directly, and the compilation works.

But if I try to link against the shared library, I get an undefined reference error:

undefined reference to `klopt_cuda_vector_set(unsigned long, float, float*, int)'

I inspected the shared library with the ‘nm’ command and the function is indeed present (marked with ‘T’).

So why am I getting this error message?

The best way is to first compile CU file into “.o” files…

Take all .o files in your project, pass it to “gcc” (Dont use link or ld here…) — gcc --shared -fPIC or whatever options and generate the .SO file…

If in doubt, try using “cmake” in verbose mode and see what command line he uses for generating shared objects… (Well, there r lots of tutorials out there that teaches u how…)