# How to make a shared library?

**URL:** <https://forums.developer.nvidia.com/t/how-to-make-a-shared-library/135248>\
**Category:** Legacy PGI Compilers (archived)\
**Created:** [March 26, 2017, 2:02am UTC](https://forums.developer.nvidia.com/t/how-to-make-a-shared-library/135248 "2017-03-26T02:02:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyfengMIT](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cyfengMIT](https://forums.developer.nvidia.com/u/cyfengMIT)\
**Post date:** [March 26, 2017, 2:02am UTC](https://forums.developer.nvidia.com/t/how-to-make-a-shared-library/135248/1 "2017-03-26T02:02:57Z")

</div>

Hi,

For using the existed cuda fotran code in Python, I tried to compile (v16.10) them as shared library on linux (CentOS 6.8).  
But I got a little trouble while compiling the simple example in step 2:

> /usr/bin/ld: /tmp/pgcudaregDucexwm0fdvc.o: relocation R\_X86\_64\_PC32 against undefined symbol `\_\_pgi\_cuda\_register\_fat\_binary’ can not be used when making a shared object; recompile with -fPIC  
> /usr/bin/ld: final link failed: Bad value  
> pgacclnk: child process exit status 1: /usr/bin/ld

Are there any suggestions ?  
Thank you in advance.

Compiling steps:  
pgf90 -c -Mcuda -fpic cuda\_so.cuf  
pgf90 -o cuda\_so.so -Mcuda -shared cuda\_so.o

cuda\_so.cuf

```plaintext
module cuda_so_f90
use iso_c_binding
use cudafor
implicit none
  character(len=30, kind=c_char), device :: d_a, d_b

contains
  subroutine test_so( ha, hb) bind(c, name="test_so")
  !DEC$ ATTRIBUTES DLLEXPORT :: test_so
  implicit none
    character(len=30, kind=c_char) :: ha, hb

    print*, 'hb into test_so :', hb
    d_a=ha
    d_b=d_a
    hb=d_b
    print*, 'hb leaving test_so :', hb

  end subroutine test_so
end module cuda_so_f90

```

---

<div class="post-metadata">

**Author:** ![MatColgrove](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/matcolgrove/32/14047_2.png) [@MatColgrove](https://forums.developer.nvidia.com/u/MatColgrove)\
**Post date:** [March 27, 2017, 2:39pm UTC](https://forums.developer.nvidia.com/t/how-to-make-a-shared-library/135248/2 "2017-03-27T14:39:01Z")

</div>

Hi cyfengMIT,

For shared objects and DLLs, you’ll need to compile without relocatable device code (RDC) enabled by adding the “-Mcuda=nordc” flag. RDC requires a device linker which is not available for dynamically loaded objects.

> % pgf90 -c -Mcuda=nordc -fpic cuda\_so.cuf  
> % pgf90 -o cuda\_so.so -Mcuda=nordc -shared cuda\_so.o

Note that without RDC, you will not be able to call device routines located in external modules, or access device data from external modules. Both require a device linker.

Hope this helps,  
Mat

---

<div class="post-metadata">

**Author:** ![cyfengMIT](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cyfengMIT](https://forums.developer.nvidia.com/u/cyfengMIT)\
**Post date:** [March 30, 2017, 10:26am UTC](https://forums.developer.nvidia.com/t/how-to-make-a-shared-library/135248/3 "2017-03-30T10:26:54Z")

</div>

Hi Mat,

It works! Thanks for your suggestion.  
And I want to make sure that does it mean I can only put all the device related data, subroutines and functions into the module cuda\_so\_f90 for making shared objects and DLLs ?  
Are there any concerns beyond this design ?

CY

---

<div class="post-metadata">

**Author:** ![MatColgrove](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/matcolgrove/32/14047_2.png) [@MatColgrove](https://forums.developer.nvidia.com/u/MatColgrove)\
**Post date:** [March 30, 2017, 2:47pm UTC](https://forums.developer.nvidia.com/t/how-to-make-a-shared-library/135248/4 "2017-03-30T14:47:12Z")

</div>

Hi CY,

I should clarify. You can’t access device data found in external modules directly. (i.e you’ve used the OpenACC “declare” directive on the external module data and/or access the device data by using the module in the kernel). If you pass the external device data as an argument to the device kernel, then it’s fine. If “cuda\_so.cuf” is self contained, then you shouldn’t have any issues.

The problem is that since there’s no device side dynamic linker, external references in device code can’t be resolved.

-Mat

---

<div class="post-metadata">

**Author:** ![cyfengMIT](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@cyfengMIT](https://forums.developer.nvidia.com/u/cyfengMIT)\
**Post date:** [April 5, 2017, 8:35am UTC](https://forums.developer.nvidia.com/t/how-to-make-a-shared-library/135248/5 "2017-04-05T08:35:22Z")

</div>

Hi Mat,

I got it. Thank you very much.

CY
