Created
December 15, 2016 22:16
-
-
Save svenk/82e48019b3b8600e82fd2c633231892c to your computer and use it in GitHub Desktop.
Some Fortran/C linking test
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
FUNCTION f1(a,b,c) | |
USE, INTRINSIC :: ISO_C_BINDING | |
INTEGER :: a, b, c | |
INTEGER :: f1 | |
REAL, PARAMETER :: f2=3, f4=5 | |
f1 = a*b*c | |
WRITE (*,*) 'fort func=a*b*c=', f1 | |
END FUNCTION f1 | |
SUBROUTINE simple(i) | |
USE, INTRINSIC :: ISO_C_BINDING | |
REAL, INTENT(IN) :: i | |
INTEGER :: a,b,c,d | |
WRITE(*,*) 'simple i=', i | |
a = 2 | |
b = 3 | |
c = 3 | |
!d = f1(a,b,c) | |
! WRITE (*,*) 'fort called func=',d | |
END SUBROUTINE simple | |
SUBROUTINE vect(V, Q) | |
USE, INTRINSIC :: ISO_C_BINDING | |
!REAL, INTENT(IN) :: V(3) | |
REAL :: V(3) | |
REAL :: Q(3) | |
WRITE(*,*) 'fortvec V(1)=', V(1) | |
WRITE(*,*) 'fortvec V(2)=', V(2) | |
WRITE(*,*) 'fortvec V(3)=', V(3) | |
WRITE(*,*) 'fortvec Q(1)=', Q(1) | |
WRITE(*,*) 'fortvec Q(2)=', Q(2) | |
WRITE(*,*) 'fortvec Q(3)=', Q(3) | |
V(3) = 5. | |
END SUBROUTINE vect | |
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 main() { | |
printf("This is cpp\n"); | |
float i=5; | |
simple_(&i); | |
float V[3]; | |
V[0] = 2.3; V[1] = 5.3; V[2] = 3.2; | |
// float Q[3] = {1,2,3}; | |
float Q[3]; | |
Q[0] = 2.3; Q[1] = 5.3; Q[2] = 3.2; | |
vect_(V, Q, 3, 3); | |
printf("V[2] = %f\n", V[2]); | |
int a=2, b=3, c=1; | |
int res = 0; | |
res = f1_(&a,&b,&c); | |
//printf("res func(%f,%f,%f) = %f\n", a,b,c,res); | |
printf("res func(%i,%i,%i) = %i\n", a,b,c,res); | |
} |
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
CC=gcc | |
F90=gfortran | |
all: | |
${CC} -Wall -c main.c | |
${F90} -c aux.f90 | |
${CC} -o test main.o aux.o -lgfortran | |
clean: | |
rm *.o test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Done in June 23, 2016.