C++ call fortran using cmd

Object: using c++ to call fortran subroutine (f77 file) using command 

Platform: windows 10, Visual Studio 2015, c++ (Intel Parrallel Studio XE 2017),  fortran (Intel Visual Fortran)


Testing code source: http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html

Fortran code ( fsub.for ): 


      subroutine fortfunc(ii,ff)
      integer ii
      real*4  ff

      write(6,100) ii, ff
 100  format('ii=',i2,' ff=',f6.3)

      return
      end


C++ code ( test.cpp ):

#include <iostream>

using namespace std;

extern"C" {
void fortfunc_(int *ii, float *ff);
}

main()
{

   int ii=5;
   float ff=5.5;

   fortfunc_(&ii, &ff);

   return 0;
}


Step 1:

command:

gfortran -c fsub.for

output: fsub.o


Step 2: 

command:

nm fsub.o

output: name of that fortran function or subroutine being called in C++ (in this example, name is fortfunc_), and then check if name in c++ code matches this name.


Step 3:

 command:

g++ test.cpp fsub.o -o call.exe -lgfortran

output: call.exe


Step 4:

command:

call.exe

output: result 


The commands and results for Step 1 to Step 4 are as shown as figure below:

path of fsub.for and test.cpp for this test is: C:\Personal\code\C 




Reference:

for f77 linking http://stackoverflow.com/questions/5663083/linking-fortran-and-c-binaries-using-gcc/41317505#41317505

for f90 linking http://stackoverflow.com/questions/19276874/mixed-c-and-fortran-linking-issue?rq=1



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值