Skip to content

Instantly share code, notes, and snippets.

@calebreister
Created September 9, 2014 04:47
Show Gist options
  • Save calebreister/b408e1d2a49a30f330c6 to your computer and use it in GitHub Desktop.
Save calebreister/b408e1d2a49a30f330c6 to your computer and use it in GitHub Desktop.
I have been messing with Fortran some lately... here is some code that allows you to call a Fortran function from a C program. It is very simple at this point
test: test.o mysum.o
gcc -o test test.o mysum.o -lgfortran
test.o: test.c
gcc -c test.c
mysum.o: mysum.f90
gfortran -ffree-form -c mysum.f90
integer function mysum(a, s)
implicit none
integer, intent(in) :: s
integer, dimension(s), intent(in) :: a
integer :: i
integer :: result = 0
do i=1, size(a)
result = result + a(i)
end do
mysum = result
end function
#include <stdio.h>
int mysum_(int* a, int* s);
int main() {
int arr[5] = {1,2,3,4,5};
int arr_size = 5;
printf("%i\n", mysum_(arr, &arr_size));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment