# linking issues between C program and cuda library

**URL:** <https://forums.developer.nvidia.com/t/linking-issues-between-c-program-and-cuda-library/13955>\
**Category:** CUDA Programming and Performance\
**Created:** [December 22, 2009, 4:44pm UTC](https://forums.developer.nvidia.com/t/linking-issues-between-c-program-and-cuda-library/13955 "2009-12-22T16:44:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![klub](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@klub](https://forums.developer.nvidia.com/u/klub)\
**Post date:** [December 22, 2009, 4:44pm UTC](https://forums.developer.nvidia.com/t/linking-issues-between-c-program-and-cuda-library/13955/1 "2009-12-22T16:44:27Z")

</div>

Hello,

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

```auto
...

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/cudafct.cu -o src/cudafct.o

nvcc -arch sm_13 -O3 --compiler-options -fno-strict-aliasing -I./include/ -I./src/ -c src/memory_management.cu -o src/memory_management.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:

```auto
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:

```auto
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:

```auto
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?

Thanks

---

<div class="post-metadata">

**Author:** ![mikola](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@mikola](https://forums.developer.nvidia.com/u/mikola)\
**Post date:** [December 23, 2009, 3:58am UTC](https://forums.developer.nvidia.com/t/linking-issues-between-c-program-and-cuda-library/13955/2 "2009-12-23T03:58:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![klub](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@klub](https://forums.developer.nvidia.com/u/klub)\
**Post date:** [December 23, 2009, 8:13am UTC](https://forums.developer.nvidia.com/t/linking-issues-between-c-program-and-cuda-library/13955/3 "2009-12-23T08:13:01Z")

</div>

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

> [@](#):
>
> one of the way to use your function from C (compiled by gcc separately), is to add directive export “C” before your function. i.e. in \*.cu

---

<div class="post-metadata">

**Author:** ![klub](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@klub](https://forums.developer.nvidia.com/u/klub)\
**Post date:** [December 23, 2009, 8:29am UTC](https://forums.developer.nvidia.com/t/linking-issues-between-c-program-and-cuda-library/13955/4 "2009-12-23T08:29:20Z")

</div>

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:

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

```
