0

I have a static library named mylib.a written in C and I am trying to do my first call to C from Fortran.

I want to invoke in Fortran the following function contained in the C library:

double get_step(double value); 

I try the following Fortran code:

! TEST FORTRAN WRAPPER

module test

        use, intrinsic  :: iso_c_binding

        ! Interface to C routine
        ! double get_step(double value);
        interface
                real(c_double) function _get_step(value) bind(C, 'get_step')
                        use, intrinsic :: iso_c_binding, only : c_double
                        real(c_double) :: value
                end function
        end interface

end module

I try to compile it like this:

$ flang test.f90 mylib.a
F90-F-0004-Unable to open MODULE file iso_c_binding.mod (test.f90: 5)
F90/x86-64 FreeBSD Flang - 1.5 2017-05-01: compilation aborted
  • What am I doing wrong?
  • Can I use the same name get_step for the Fortran function?
M.E.
  • 4,955
  • 4
  • 49
  • 128
  • 1
    You don't show a Fortran function called `get_step`, or indeed any Fortran function. Do you want to provide one, or are you asking about the interface name? – francescalus Jun 23 '21 at 22:38
  • I do not know, I am trying to wrap the C function with a Fortran function that will be later be recognised by f2py. – M.E. Jun 23 '21 at 22:52
  • 3
    You have several problems. First, remove the hyphen from `_get_step` in the `function` statement. Second, you need to add the `value` attribute to the declaration of `value`. That is, use `real(c_double), value :: value`. Third, are you using flang from the FreeBSD ports collections? If yes, you might as well beat your head against a wall. – steve Jun 23 '21 at 23:19
  • I have removed the hyphen from the function statement, I have added value attribute and I understand that it is done because the variable is passed by value. I have moved to gfortran as it seems that flang does not support iso_c_binding. Yes, I was using flang from the ports. By doing that I managed to get this compiled, now the problem is that when I try to get it compiled into a shared object with f2py it does not work. I will try to prepare a separate question for that – M.E. Jun 23 '21 at 23:28
  • Can't help with f2py. – steve Jun 24 '21 at 00:43
  • Yes, prepare a separate question. Be aware that f2py only supports a subset of Fortran. Many modern features are not supported. It is basically meant for legacy codes. – Vladimir F Героям слава Jun 24 '21 at 06:15
  • 1
    https://github.com/flang-compiler/f18/issues/318 may be relevant - "f18 will support all the standard modules. It does not support them yet." From 2019 – Ian Bush Jun 24 '21 at 07:49
  • 1
    @IanBush, wrong flang. The version available through FreeBSD port collection is 7.0, which is/was The Portland Group, Inc's attempt at porting PGI Fortran to LLVM. This was essentially abandoned when NVidia acquired PGI, and decided that it would be better to start over with development of a new Fortran frontend. – steve Jun 24 '21 at 16:03
  • @steve That's one of the reasons behind "may", I've lost track and interest with flang when the earlier stuff was IMO unusable. If the newer stuff looks better I may regain interest. – Ian Bush Jun 24 '21 at 16:13
  • Related to this is: https://stackoverflow.com/questions/68121634/jupyter-notebook-kernel-crash-when-trying-to-encapsulate-two-c-objects-using-f2p – M.E. Jun 24 '21 at 19:21

0 Answers0