CUDA shared library created, but the functions can't be found.

To simplify things:I have a CUDA file

#libcuLSE.cu

#include <stdio.h>

void hello(void)

{

	printf("Hello World!!!");

}

which I compile into the shared library the following way:

$ nvcc --shared -o libcuLSE.so libcuLSE.cu --compiler-options '-fPIC' -arch sm_13

The command above executes just fine.

After that I’m trying to call it from the main function:

#main.c

#include <stdlib.h>

#include <stdio.h>

#include <dlfcn.h>

int main(void)

{

	void *h;

	void (*hello)(void);

	h = dlopen("libcuLSE.so",RTLD_LAZY);

	hello = dlsym(h,"hello");

	hello();

}

When I try to run it, I’m getting the following error.

$gcc -o out main.c -ldl -L.

$ ./out 

Segmentation fault

So the main function loads the shared library (otherwise it would complain that it can’t find it), but the pointer is not pointing to the “hello” function. If I run nm, I can see that hello is in the shared object:

nm libcuLSE.so | grep hello

0000000000001152 t _GLOBAL__I__Z5hellov

00000000000024ee T _Z5hellov

I tried to compile the libcuLSE using gcc compiler

gcc -c -fPIC libcuLSE.c -o libcuLSE.o

gcc -shared -o libcuLSE.so libcuLSE.o

and then call that library. It works just fine…

I assume I’m missing something in the creation of the shared CUDA library…

But what it is exactly, I have no idea… I attached a tar file with all the files I used and a Makefile.

Any ideas?
test_forum.tar (50 KB)

Looks like the way things are compiled above is wrong.

use extern “C”…

and possibly check if you have a __cdecl vs __stdcall calling convention issue?

Christian

I thought in Linux __cdecl is assumed and doesn’t have to be specified.

If anyone ever successfully compiled and used CUDA shared objects, can you share the compilation string you used?

You need to use extern “C”.

wow… :"> thank you very much. I was suspecting that it’s some minor issue…

everything works fine now.