1.Test1.C文件
void test_simple();
void test_call(char filename[], int a[], int n, int* val);
void test_call(char filename[], int a[], int n, int* val)
{
printf("%s\t%d\n", filename, n);
}
void test_simple()
{
printf("%s\t%d\n", "test_simple", 123456);
}
2.Test2.f90文件
program name
use,intrinsic::iso_c_binding
implicit none
character(80) str
integer, parameter:: n=3
integer aa(n), s
!函数接口
interface
subroutine test_call(filename,a,n,s) bind(c,name='test_call')
character filename(*)
integer,value:: n
integer aa(n), s
end subroutine
end interface
interface
subroutine test_simple() bind(c)
end subroutine
end interface
aa = [1,2,3]
str = "1.txt" // C_NULL_CHAR
call test_call(str,a,n,s)
call test_simple()
end program
3.用gfortran编译c和Fortran
D:\Soft\mingw64\bin\gfortran.exe -c Test1.c
D:\Soft\mingw64\bin\gfortran.exe -c Test2.f90
会产生Test1.o和Test2.o两个文件
4.链接
D:\Soft\mingw64\bin\gfortran.exe Test1.o Test2.o -o Test.exe
5.也可以把C文件编译成lib库,再和Fortran链接
D:\Soft\mingw64\bin\ar.exe rv Test1.a Test1.o
D:\Soft\mingw64\bin\gfortran.exe Test2.o Test1.a -o Test.exe
这个地方一定要注意文件的顺序, .o文件一定要放到.a的前面,否则将编译错误: undefined reference to `test_call'