I am trying to run a C function from a Linux shared library (.so) from Fortran. The function in C receives an integer number from the Fortran program. I am not getting an erroneous value in C when using the intel compiler (2021.3.0), yet gfortran works without any issues. There seems to be a type error that I can't identify. I am creating an abstract interface following the syntax for interoperability established here
Function in C:
int print_number(int n)
{
printf("Hello world! %d\n", n);
return 0;
}
Function in Fortran
! Interface with shared library
abstract interface
!% -------------------------------------------------------------------------------
!% Simulation
!% -------------------------------------------------------------------------------
integer function print_number(number)
use, intrinsic :: iso_c_binding
implicit none
integer(c_int), value :: number
end function print_number
end interface
The output when calling print_number(2):
Hello world! 734920112
I am attaching the files to reproduce the error here. First, execute ./compile.sh
, then ./run_test
I would appreciate it a lot if someone could point out what I am doing wrong.
Thanks!
CodePudding user response:
You should not use an abstract interface for this, but the problem probably comes from not using bind(C)
.
Without bind(C)
Intel Fortran compiles the function to expect a pointer (to a copy of something). To make it expect a value, use both value
and bind(C)
.