C call subroutine in Fortran module

May I ask how C call subroutine in Fortran module implemented in PGI Fortran?

Could you please provide a simple example as well.

Tuan

Hi Tuan,

Calling a Fortran module routine would follow the same conversions as normal C to Fortran calling (See Chapter 12 of the PGI User’s Guide). Make sure that you use F77 calling conventions or ISO_C_BINDING in your Fortran module. Also, PGI symbol naming for module routines is “”, all lower case. Other compilers may have different naming conventions. Note that the ‘nm’ utility (or ‘pgnm’ on Windows), will show you the symbol names in an object file.

A very simple example:

$ cat testf.f90 
module testf

  integer :: mod = 2
   
  contains 

  subroutine plusMod(i) 
      integer :: i
      i = i + mod
  end subroutine plusMod

end module testf

$ cat test.c
#include <stdio.h>

void testf_plusmod_(int *);

void main () {

  int i = 1;
  testf_plusmod_(&i);
  printf("%d\n", i);

}
$ pgf90 -c testf.f90
$ pgcc -pgf90libs test.c testf.o
test.c:
$ a.out
3
$ nm testf.o
0000000000000000 A ..Dm_testf
000000000000001e t __testf_plusmod_END
0000000000000000 D _testf_8_
                 U pgf90_compiled
0000000000000000 T testf_
0000000000000010 T testf_plusmod_

Hope this helps,
Mat

Thanks a lot, Mat, for the detailed explanation.

There is another issue: I use VisIt visualization tool. When I try to make call to the functions from libsimV2f.a library, it’s okay if I declare

external visitdetectinput
integer visitdetectinput

If I try to use F03 BIND© feature

     INTEGER(c_int) FUNCTION visitdetectinput(blocking, consoledesc) BIND(C, name="visitdetectinput_") 
       USE, INTRINSIC ::  iso_c_binding
       INTEGER(c_int), VALUE :: blocking, consoledesc
     END FUNCTION visitdetectinput

A segmentatioin fault occurs. What can cause this problem Mat?

I used nm utility and this is the result

nm -A libsimV2f.a | grep visitdetectin
libsimV2f.a:visitfortransimV2interface.c.o:0000000000000980 T visitdetectinput_

Thanks,
Tuan

My best guess is that you need to pass by reference not by value. Though, if you can post the prototype for “visitdetectinput”, that would be helpful.

  • Mat