Created
September 9, 2014 04:47
-
-
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
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
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 |
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 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 |
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 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