Call Fortran lib from C++

本文详细介绍了如何在C++项目中整合Fortran源代码,重点讨论了使用IntelliFortran创建静态库,确保函数命名及参数传递正确,以及C++与Fortran之间的数据类型映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近项目遇到的需求是 将 fortran 源代码 放入现有的 cpp 项目中, 几次尝试都遇到问题: 

目前, fortran 代码 还都是 funciton 和 subroutine, 不涉及 for95之后的 type 以及 指针 这些新功能。

这里记录几个关键点:

1. intell fortran 需要create static lib 的project, 注意 不是 dynamic library! 

2. source 文件中 fortran 函数 和 子函数 务必都用小写! 

   采用 CDEC$ ATTRIBUTES DLLEXPORT, STDCALL 来标明 函数名称 和 input变量名称; 

     以下是用于测试的 fortran 代码: 

三个 函数和子程序 尝试 传入 double, double[]。

一定注意缩进;

      subroutine test(b,a,c)
CDEC$ ATTRIBUTES DLLEXPORT, STDCALL :: test    
CDEC$ ATTRIBUTES REFERENCE :: b
CDEC$ ATTRIBUTES REFERENCE :: a
CDEC$ ATTRIBUTES REFERENCE :: c
    
      integer :: a
      integer :: b,c 
      c=a+b     
      end
      
      subroutine test2(d,e)
CDEC$ ATTRIBUTES DLLEXPORT, STDCALL :: test2
CDEC$ ATTRIBUTES REFERENCE :: d
CDEC$ ATTRIBUTES REFERENCE :: e
      real(8) :: d(2)
      real(8) ::  e
      
      e = d(1)+d(2)
      
      end
      
      real(8) function funcadd(a,b)
CDEC$ ATTRIBUTES DLLEXPORT, STDCALL :: funcadd
CDEC$ ATTRIBUTES REFERENCE :: a
CDEC$ ATTRIBUTES REFERENCE :: b
      real(8) :: a
      real(8) :: b

      funcadd = a+b
     
      end function funcadd

3. cpp create console project 时 32位 还是 64位 要与 fortran project 一致

 C++ project property 设置:

a. 添加intell fortran lib 的文件夹 (看你compiler 装在哪里):  

C:\Program Files (x86)\compilers_and_libraries_2016.3.207\windows\compiler\lib\ia32_win 

b. 在 Linker->Input-> Addtional Dependencies 添加 fortran静态库 (包括文件路径); 

c. CPP 头文件(useForLib.h)中 添加声明:

extern "C" { extern void __stdcall test(int* a, int* b, int* c); }
extern "C" { extern void __stdcall test2(double* a, double* b); }
extern "C" { extern double __stdcall funcadd(double* a, double* b); }

d. CPP 代码: 

#include "stdafx.h"
#include "useForLib.h"
# include <iostream>

using namespace std; 


double GetSum(double* a, double*b )
{
    return a[0] + a[1]+ *b;
}

int main()
{   
    char s; 
    
    int numA = 3;
    int *a=&numA;
    int numB = 4; 
    int *b = &numB;
    int numC;
    int *c = &numC;

    test(a,b,c);
    cout << " " << *a << endl;
    cout << " " << *c << endl;    
    double* Myarray = new double[2];
    Myarray[0] = 100;
    Myarray[1] = 200;
    double cc = 0;
    double bb = 0.34;

    cout << "before call fortran func: " << cc << endl;
    test2(Myarray, &cc);
    cout << "after call fortran func: " << cc << endl;
    
    cc = 0;
    cout << "before call cpp func: " << cc << endl;
    cc = GetSum(Myarray,&bb);
    cout << "after call cpp func cc: " << cc << endl;
    cout << "after call cpp func bb: " << bb << endl;

    double sumNum = 0; 
    double af = 1;
    double bf = 2;
    sumNum = funcadd(&af,&bf);

    cout << "after call cpp FuncAdd sum: " << sumNum << endl;


    cin>>s;
    
    return 0;
}

测试一下: 

 可以 运行 得到结果 

 再补充一点,fortran 中的 double complex 类型对应 C++中的长度为2的一维 数组 传递 值时,务必传递指针定义。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenxin0215

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值