[ncl-talk] Unable to link to Fortran shared object created with WRAPIT

Dennis Shea shea at ucar.edu
Sun May 13 08:27:29 MDT 2018


Hello,

Once a function/subroutine is placed in a module, two 'things' must be done:

[1] A module must be compiled *prior* to its invocation by other program
units
[2] The program unit(s) invoking the function/subroutine contained within a
module must apply the USE statement.

Attached are two fortran codes. Note the module is compiled 1st.
Also, a side point, these could be in one file as long as the module is
compiled prior to other units.

%> gfortran toy_module.f90 test.toy_module.f90
%> ./a.out

 z=   6.8999996

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Likely, you already know the above .
However, I thought it should be mentioned for others who might want to do
something similar to what you are doing.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

OK ... how to accommodate using a subroutine contained within a module from
NCL.

[1] *THE* issue is that NCL's  WRAPIT  understands only f77 syntax.
[2] The  'USE' statement is f90. Further, it must/should appear even before
an 'implicit none' statement.
[3] WRAPIT would likely fail (untested if this is true) if the USE
statement were encountered between the 'C NCLFORTSTART' and 'C NCLEND'
delimiters.
[4] My opinion is that you must add a 2nd subroutine ... a bit of a
nuisance.

---
In the following, the 77 and 90 suffixes are used to differentiate the
program subroutines. They have no language meaning.

The 1st subroutine [mult77]  contains the WRAPIT recognized delimiters:  'C
NCLFORTSTART' and 'C NCLEND'.  WRAPIT will create the necessary
infrastructure for argument passing between arguments between NCL  (written
in C) and fortran.

The 2nd subroutine is a valid program unit invokes the 'USE' statement.

Again, the USE statement could not be simply added to the 1st subroutine
because WRAPIT would not know what to do with it!

C NCLFORTSTART
      subroutine* mult77*(x,y,z)
      implicit none
      real x,y,z
C NCLEND
      call *mult90*(x,y,z)
      return
      end
C------------
      subroutine *mult90*(x,y,z)
      *use module_mult*
      implicit none
      real, intent(in)  :: x,y
      real, intent(out) :: z

      call *mult*(x,y,z)
      return
      end

================
Test the NCL call to invoke the module. Note the module is compiled 1st.

%> *WRAPIT* toy_module.f90 toy_mult7790.f

This will create '*toy_module.so*'

The following 'toy_test.ncl' contains:

external TOY_MODULE "./toy_module.so"


   xNCL = 2.3
   yNCL = 3.0
   zNCL = 1e20
   *TOY_MODULE::mult77*(xNCL, yNCL, zNCL)  ; interface with f77 which calls
f90
   print("zNCL="+zNCL)

=====
%> ncl toy_test.ncl

(0)    zNCL=6.9

==================
Hopefully, this is somewhat clear.

Good luck

On Fri, May 11, 2018 at 3:47 PM, Michael Toy <toy at ucar.edu> wrote:

> Hello,
>
> I am trying to call a fortran subroutine from an NCL script, and have
> followed the documentation for “WRAPIT”.  This is my first time trying it.
> When I enter the following in NCL:
> ncl 0> external MLT “./mult.so"
>
> I get the error message:
> warning:An error occurred loading the external file ./mult.so, file not
> loaded
> ./mult.so: undefined symbol: mult_
> warning:error at line 0
>
>
> I’m testing a simple subroutine which multiplies two numbers together.  My
> Fortran 90 code (mult.f90) is:
>
> module module_mult
>
> contains
>
> subroutine mult(x,y,z)
>
> implicit none
>
> real, intent(in) :: x,y
> real, intent(out) :: z
>
> z = x*y
>
> end subroutine mult
>
> end module module_mult
>
>
> My “stub” file is:
> C NCLFORTSTART
>       subroutine mult(x,y,z)
>       real x,y,z
> C NCLEND
>
>
> When I run “WRAPIT mult.stub mult.f90” it seems to run successfully, and
> produces the file:
> mult.so
>
>
> I am running NCL version 6.4.0 on the following system:
> Linux x86_64 x86_64 x86_64 GNU/Linux
>
> I know this problem has come up in the NCL users’ forum before, but I’ve
> not seen how the issue gets resolved.  Your help would be appreciated.
> Thanks.
>
> Best regards,
> Mike
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk at ucar.edu
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180513/2cdc7ad1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: toy_module.f90
Type: application/octet-stream
Size: 464 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180513/2cdc7ad1/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.toy_module.f90
Type: application/octet-stream
Size: 213 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180513/2cdc7ad1/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: toy_mult7790.f
Type: application/octet-stream
Size: 337 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180513/2cdc7ad1/attachment-0002.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: toy_test.ncl
Type: application/octet-stream
Size: 148 bytes
Desc: not available
URL: <http://mailman.ucar.edu/pipermail/ncl-talk/attachments/20180513/2cdc7ad1/attachment-0003.obj>


More information about the ncl-talk mailing list