Christian
Christian

Reputation: 749

Use Fortran-code in C

I try to use a fortran-routine in C, but I doesn't work. I don't know where I made a mistake. Here my Fortran-code including the Integration-Module, which I want to use in C:

module integration
  implicit none

contains

  function Integrate(func, a,b, intsteps) result(integral)

    interface
      real function func(x)
        real, intent(in) :: x
      end function func
    end interface

    real :: integral, a, b
    integer :: intsteps
    intent(in) :: a, b, intsteps
    optional :: intsteps

    real :: x, dx
    integer :: i,n
    integer, parameter :: rk = kind(x)

    n = 1000
    if (present(intsteps)) n = intsteps

    dx = (b-a)/n

    integral = 0.0_rk
    do i = 1,n
      x = a + (1.0_rk * i - 0.5_rk) * dx
      integral = integral + func(x)
    end do

    integral = integral * dx
  end function

end module integration


real(c_float) function wrapper_integrate(func,a,b, intsteps) result(integral) bind(C, name='integrate')
  use iso_c_binding
  use integration

  interface
    real(c_float) function func(x) bind(C)
      use, intrinsic :: iso_c_binding
      real(c_float), intent(in) :: x
    end function func
  end interface

  real(c_float) :: a,b
  integer(c_int),intent(in) :: intsteps
  optional :: intsteps
  if (present(intsteps)) then
    integral = Integrate(func,a,b,intsteps)
  else
    integral = Integrate(func,a,b)
  endif

end function wrapper_integrate

and my C-Code:

#include <stdio.h>
#include <math.h>

float sin2(float x) {
  return sin(x) * sin(x);
}

float integrate(float(*func)(float), float a, float b, int intsteps);

int main() {
  float integral;
  integral = integrate(sin2,0.,1.,10000);
  printf("%f",integral);
  return 0;
}

if I execute

g++ -c main.c
gfortran -c integration.f95
g++ main.o integration.o

I get

undefined reference to `integrate(float (*)(float), float, float, int)'

Does anyone know how to handle this?

Upvotes: 2

Views: 954

Answers (3)

user1357713
user1357713

Reputation:

The OP used the gnu c++ compiler. Here's a solution, using information from several respondants, that worked, using the c++ compiler -- not the c compiler -- for me:

file 'integration.f95':

module integration
  implicit none

contains

  function Integrate(func, a, b, intsteps) result(integral)

    interface
      real function func(x)
        real, intent(in) :: x
      end function func
    end interface

    real :: integral, a, b
    integer :: intsteps
    intent(in) :: a, b, intsteps
    optional :: intsteps

    real :: x, dx
    integer :: i,n
    integer, parameter :: rk = kind(x)

    n = 1000
    if (present(intsteps)) n = intsteps

    dx = (b-a)/n

    integral = 0.0_rk
    do i = 1,n
      x = a + (1.0_rk * i - 0.5_rk) * dx
      integral = integral + func(x)
    end do

    integral = integral * dx
  end function

end module integration

real(c_float) function wrapper_integrate(func, a, b, intsteps) result(integral) bind(C, name='integrate')
  use iso_c_binding
  use integration

  abstract interface
    function iFunc(x) bind(C)
      use, intrinsic :: iso_c_binding
      real(c_float) :: iFunc
      real(c_float), intent(in) :: x
    end function iFunc
  end interface

  type(C_FUNPTR), INTENT(IN), VALUE :: func
  real(c_float) :: a,b
  integer(c_int),intent(in) :: intsteps
  optional :: intsteps

  procedure(iFunc),pointer :: myfunc
  call c_f_procpointer(func, myfunc)

  if (present(intsteps)) then
    integral = Integrate(myfunc,a,b,intsteps)
  else
    integral = Integrate(myfunc,a,b)
  endif

end function wrapper_integrate

file 'main.c':

#include <stdio.h>

#define _USE_MATH_DEFINES

#include <math.h>

float sin2(float *x) {
  return sin(*x) * sin(*x);
}

float integrate(float(*func)(float*), float* a, float* b, int* intsteps);

int main() {

  int intsteps=10000;

  float integral;
  float a=0.;
  float b=3.1416;

  integral = integrate(sin2, &a, &b, &intsteps);
  printf("The numerical value of \\int_0^\\pi dx sin^2x = %f\n",integral);
  printf("The exact value of \\int_0^\\pi dx sin^2x = %f\n",M_PI_2);

  return 0;

}

file 'compile.txt':

gcc -c main.c
gfortran -c integration.f95
g++ -o intsinq main.o integration.o

Upvotes: 0

Alexander Vogt
Alexander Vogt

Reputation: 18098

If you are using the module ISO_C_Binding, you can directly passing a function from C to Fortran as a function pointer C_FUNPTR. See here for details.

In your case, this would look like:

real(c_float) function wrapper_integrate(func, a, b, intsteps) result(integral) bind(C, name='integrate')
  use iso_c_binding
  use integration

  abstract interface
    function iFunc(x) bind(C)
      use, intrinsic :: iso_c_binding
      real(c_float) :: iFunc
      real(c_float), intent(in) :: x
    end function iFunc
  end interface

  type(C_FUNPTR), INTENT(IN), VALUE :: func
  real(c_float) :: a,b
  integer(c_int),intent(in) :: intsteps
  optional :: intsteps

  procedure(iFunc),pointer :: myfunc
  call c_f_procpointer(func, myfunc)

  if (present(intsteps)) then
    integral = Integrate(myfunc,a,b,intsteps)
  else
    integral = Integrate(myfunc,a,b)
  endif

end function wrapper_integrate

Obviously, your solution is more elegant ;-)

Also, please note that Fortran passes variables by reference (unless you specify the VALUE attribute, which you don't). Therefore, you need to change you C code accordingly:

#include <stdio.h>
#include <math.h>

float sin2(float *x) {
  return sin(*x) * sin(*x);
}

float integrate(float(*func)(float*), float* a, float* b, int* intsteps);

int main() {
  float integral;
  float a=0.;
  float b=1.;
  int intsteps=10000;


  integral = integrate(sin2, &a, &b, &intsteps);
  printf("%f",integral);
  return 0;
}

Upvotes: 5

Michel Billaud
Michel Billaud

Reputation: 1826

You are using the C++ compiler, not the C one. Linking conventions may be different.

And you forgot to link with the math library (because of sin)

gcc -c main.c
gfortran -c integration.f95
gcc main.o integration.o  -lm

Upvotes: 1

Related Questions