用gfortran编译C和Fortran

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'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值