fortran中调用C封装的函数

来源:互联网 发布:win7怎么禁止安装软件 编辑:程序博客网 时间:2024/05/21 16:21

hello.c: 功能实现
------------------------------------------------------------                                                       
#include<stdio.h>

#ifdef PGI //编译时使用 -DPGI 选项定义宏 PGI
void hello_(int *num, char *name) //fortran中调用,注意参数要使用指针. 这里函数名需要写成 hello_,末尾多一个下划线.
#else
void hello(int *num, char *name) //c中调用
#endif
{
    printf("hello: %d, %s\n", *num, name);
}
------------------------------------------------------------

fhello.f: 提供fortran接口
------------------------------------------------------------
      subroutine fhello(num, name)
        call hello(num, name) !调用C中封装的 hello_ 函数,注意下划线的作用 
      end
------------------------------------------------------------

Makefile: 编译
------------------------------------------------------------
test:
        f77 test.f -L./lib -lftest
f:
        gcc -c -fpic -DPGI hello.c # 定义宏 PGI,使得正常编译
        f77 -c -fpic fhello.f
        ar -r lib/libftest.a hello.o fhello.o   
clean:
        rm -f *.o
------------------------------------------------------------

test.f: 测试函数
------------------------------------------------------------
        call fhello(2, 'zkl')
      end
------------------------------------------------------------


原创粉丝点击