linking issues between C program and cuda library


I am currently developping a CUDA static library. This library contains C++ and CUDA source code. Here an extract of the make process:


g++ -DHAVE_CONFIG_H -I.  -I./include/  -I./src/	-g -O2 -MT my_gpu_C.o -MD -MP -MF .deps/my_gpu_C.Tpo -c -o my_gpu_C.o `test -f 'src/bindings/C/my_gpu_C.cpp' || echo './'`src/bindings/C/my_gpu_C.cpp


nvcc -arch sm_13 -O3 --compiler-options -fno-strict-aliasing -I./include/  -I./src/  -c src/ -o src/cudafct.o


ar cru libmy_gpu.a my_gpu_C.o fct_call_implementation.o localqueu.o init_network.o manage_cpu_affinity.o read_conf_file.o set_repartition.o src/cudafct.o src/memory_management.o

This actually produce libmy_gpu.a

In my_gpu_C.cpp, there is a function mygpu_init().

I can see it with:

nm --print-armap libmy_gpu.a | grep sg_init

23:mygpu_init in my_gpu_C.o

But when I am trying with a simple C code, I have this error during the link process:

gcc -I../include -L.. -lmy_gpu  hello.c -o hello

/tmp/ccoSdBbg.o: In function `main':

hello.c:(.text+0x1d): undefined reference to `mygpu_init'

collect2: ld returned 1 exit status

However, everything is fine when I compile with nvcc:

nvcc -I../include -L.. -lmy_gpu  hello.c -o hello

is OK.

Am I missing something? Is it possible to compile a simple C program with gcc, when all the CUDA function are in the library?


one of the way to use your function from C (compiled by gcc separately), is to add directive extern “C” before your function. i.e. in *.cu

extern “C” int MyFunct()
return 1;

then you can use it fro plain C.

another way is to force nvcc to compile host part as C, however it is not working for me.

The function I want to call is in a .cpp file. And it is already surounded with a extern “C”.

OK, that was a noob error. Simply because I did not write the -ls_gpu at the end of my compile command… However, it seems nvcc do not care where the -llibrary is placed. Here the correct command:

gcc -I../include -L.. hello.c -o hello -ls_gpu -lcudart