混合编程:从C调用Fortran [英] Mixed programming: Calling FORTRAN from C

查看:254
本文介绍了混合编程:从C调用Fortran的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做一个概念证明从C / C调用子程序FORTRAN ++。
我不知道我在正确的方向,请指导我....

我是什么...

我写了下面的FORTRAN code

  INTEGER * 4函数fact(N)
INTEGER * 4 N
INTEGER * 4我,AMT
AMT = 1
我= 1,N
AMT = AMT *我
END DO
事实上= AMT
结束SUBROUTINE毕达哥拉斯(A,B,C)
REAL * 4
REAL * 4 B
REAL * 4℃
C = SQRT(A * A + B * B)
结束

采用G77为 g77.exe -c FORTRANfun.for

它编译

我写后续的c code ...

 的#include<&stdio.h中GT;EXTERN INT __stdcall FACT(INT N);
EXTERN无效__stdcall勾股定律(浮动,浮动B,浮动* C);主要()
{
    浮℃;
    的printf(7阶乘是:%d个\\ N,FACT(7));
    PYTHAGORAS(30,40,和C);
    的printf(如果斜边边30,40:%F \\ N,C);
}

使用Visual Studio C编译器为 CL / C new.c

它编译

当我试图联系起来,为 LINK new.obj FORTRANfun.o
我收到以下错误...

  new.obj:错误LNK2019:​​解析外部符号_FACT @ 4在功能上引用_main
new.obj:错误LNK2019:​​解析外部符号_PYTHAGORAS @函数_main引用12
new.exe:致命错误LNK1120:2无法解析的外部


解决方案

在Zeeshan 回答,你必须使用指针传递变量的Fortran:

 的extern INT __stdcall事实为(int * N);
EXTERN无效__stdcall毕达哥拉斯(浮动*一,浮动* B,*浮动C);

I have to do a proof of concept on calling FORTRAN subroutines from C/C++. I don't know what I am in right direction, please guide me....

What I did is...

I wrote the following FORTRAN code

INTEGER*4 FUNCTION Fact (n)
INTEGER*4 n
INTEGER*4 i, amt
amt = 1
DO i = 1, n
amt = amt * i
END DO
Fact = amt
END

SUBROUTINE Pythagoras (a, b, c)
REAL*4 a
REAL*4 b
REAL*4 c 
c = SQRT (a * a + b * b)
END

compiled it using g77 as g77.exe -c FORTRANfun.for

I wrote following c code...

#include <stdio.h>

extern int __stdcall FACT (int n);
extern void __stdcall PYTHAGORAS (float a, float b, float *c);

main()
{
    float c;
    printf("Factorial of 7 is: %d\n", FACT(7));
    PYTHAGORAS (30, 40, &c);
    printf("Hypotenuse if sides 30, 40 is: %f\n", c);
}

compiled it using Visual Studio C compiler as cl /c new.c

When I tried to link, as LINK new.obj FORTRANfun.o I am getting the following error...

new.obj : error LNK2019: unresolved external symbol _FACT@4 referenced in function _main
new.obj : error LNK2019: unresolved external symbol _PYTHAGORAS@12 referenced in function _main
new.exe : fatal error LNK1120: 2 unresolved externals

解决方案

On top of Zeeshan answer, you have to use pointers for passing variables to Fortran:

extern int __stdcall fact(int* n);
extern void __stdcall pythagoras(float* a, float* b, float *c);

这篇关于混合编程:从C调用Fortran的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆