Created
July 11, 2018 23:31
-
-
Save belmarca/66ff53cba1f9b5ec45a5c1aec6e15410 to your computer and use it in GitHub Desktop.
Calling Fortran from C using iso_c_binding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: | |
gcc -c sum_to_n.c | |
gfortran sum_to_n.f90 sum_to_n.o -o sum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int f_sum_to_n(int *n); | |
int main () | |
{ | |
int n; | |
n = 1000; | |
printf("%i\n", f_sum_to_n(&n)); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
integer function sum_to_n(n) result(sum) bind(C, name="f_sum_to_n") | |
use iso_c_binding | |
implicit none | |
integer, intent(in) :: n | |
integer :: i, s | |
s = 0 | |
do i = 1, n | |
s = s + i | |
end do | |
sum = s | |
end function sum_to_n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment