繁体   English   中英

混合C ++和Fortran代码出现问题

[英]Issue with mixed C++ and Fortran code

我的Fortran程序有一个奇怪的问题。 以下是有关我的应用程序的一些详细信息:

  • 使用ALLOCATABLE数据结构保存信息

  • 与C ++ dll接口,该dll返回指向双精度数组的指针。 我使用固有方法c_f_pointer将C ++数组指针转换为Fortran指针

  • 从XML文件读取并将输出写入XML文件和两个文本文件。

我正在使用gfortran编译Fortran和Simply Fortran IDE。 在C ++方面,我正在使用gcccode :: Blocks 该应用程序的构建没有任何问题,并在调试模式下运行。

但是,当我双击应用程序可执行文件以查看此类启动将执行什么操作时,它没有响应。 我不得不去任务管理器并杀死该程序。 我尝试了多次,每次都发生了。 我删除了由应用程序&gmon.out创建的输出文件,然后双击可执行文件,瞧,程序再次运行。

它可能会运行几次,并且会随机冻结。 然后,我重复上述过程,有时这会使应用程序恢复活力。 我真的迷失了这里的问题。 这是内存管理不善的过程吗? 与C ++指针和Fortran指针的处理方式有关吗?

我认为调试版本可能存在问题,因此我将项目更改为选项,以不包括调试变量,但问题仍然存在。

我可以尝试制作一个简单的程序来演示该问题(如果需要)。

任何想法/帮助/建议都非常感谢。

*新的信息

我尝试按照评论中的要求创建一个小的工作示例,与此同时,我解决了该问题。 但是我不知道它解决的原因。 因此,为了更好地理解它,我将CPP标头和源文件放在这里:

DLL文件

#ifndef DLL_H_INCLUDED
#define DLL_H_INCLUDED

#include <windows.h>
#include <stdio.h>
#include <math.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else

#endif

extern "C"
{

double DLL_EXPORT __cdecl *Function1(int InputCombination, double Input1, double Input2, double Pressure);
double DLL_EXPORT __cdecl Function2(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function3(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function4(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function5(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function6(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function7(double DBTemperature, double WBTemperature, double Pressure);
double DLL_EXPORT __cdecl Function8(double DBTemperature, double RelHumidity, double Pressure,int LoadLibraryFlag=1);
double DLL_EXPORT __cdecl Function9(double DBTemperature, double HumRatio, double Pressure);
double DLL_EXPORT __cdecl Function10(double DBTemperature, double Enthalpy, double Pressure);

}

double DB,WB,HR,RH,DP,SpVOL,ENTH;
const double CtoK = 273.15;
const double KtoC = -273.15;

#endif // DLL_H_INCLUDED

DLL文件

#include "DLL.h"

typedef double (__cdecl *fp_BisectionRootFinderTYPE) (double, double, double, double, double*, int, double(*)(double,double*));

extern fp_BisectionRootFinderTYPE BisectionRootFinder;

fp_BisectionRootFinderTYPE BisectionRootFinder;

double DLL_EXPORT __cdecl *CalcAirProperties(int InputCombination, double Input1, double Input2, double Pressure)
{
/*
 Input Combination:
    1: Given DB [C], WB [C] and Pressure [kPa]
    2: Given DB [C], RH and Pressure [kPa]
    3: Given DB [C], phi and Pressure [kPa]
    4: Given DB [C], DP [C] and Pressure [kPa]
*/

    double *AirProperties = new double[7];
/*
  Air Properties:
    [0]: Dry bulb Temperature [C]
    [1]: Wet bulb temperature [C]
    [2]: Humidity ratio []
    [3]: Relative Humidity []
    [4]: Dew point Temperature [C]
    [5]: Specific Volume [m3/kg_da]
    [6]: Enthalpy [kJ/kg_da]
*/

    HINSTANCE MathRoutinesInstance;

    MathRoutinesInstance = LoadLibrary("./MathRoutines.dll");

    BisectionRootFinder = (fp_BisectionRootFinderTYPE) GetProcAddress(MathRoutinesInstance,"BisectionRootFinder");

    if(InputCombination == 1)
    {
      DB = Input1;
      WB = Input2;
      HR = Function2(DB,WB,Pressure);
      RH = Function3(DB,WB,Pressure);
      DP = Function4(DB,WB,Pressure);
      SpVOL = Function5(DB,WB,Pressure);
      ENTH = Function6(DB,WB,Pressure);
    }
    else if(InputCombination == 2)
    {
      DB = Input1;
      RH = Input2;
      WB = Function8(DB,RH,Pressure,1);
      HR = Function2(DB,WB,Pressure);
      DP = Function4(DB,WB,Pressure);
      SpVOL = Function5(DB,WB,Pressure);
      ENTH = Function6(DB,WB,Pressure);
    }
    else if(InputCombination == 3)
    {
      DB = Input1;
      HR = Input2;
      RH = Function7(DB,HR,Pressure);
      WB = Function8(DB,RH,Pressure,1);
      DP = Function4(DB,WB,Pressure);
      SpVOL = Function5(DB,WB,Pressure);
      ENTH = Function6(DB,WB,Pressure);
   }
    else if(InputCombination == 4)
    {
      DB = Input1;
      ENTH = Input2;
      HR = HumRatfn_DB_Enthalpy_Pressure(DB,ENTH,Pressure);
      RH = RelHumidityfn_DB_HumRat_Pressure(DB,HR,Pressure);
      WB = WBTempfn_DB_RH_Pressure(DB,RH,Pressure,1);
      DP = Function4(DB,WB,Pressure);
      SpVOL = Function5(DB,WB,Pressure);
   }
   else
   {
     DB = Input1;   // + KtoC
     WB = Input2;   // + KtoC
     HR = HumRatfn_DB_WB_Pressure(DB,WB,Pressure);
     RH = RelHumidityfn_DB_WB_Pressure(DB,WB,Pressure);
     DP = DewPointTempfn_DB_WB_Pressure(DB,WB,Pressure);
     SpVOL = SpecVolumefn_DB_WB_Pressure(DB,WB,Pressure);
     ENTH = Enthalpyfn_DB_WB_Pressure(DB,WB,Pressure);
   }

    AirProperties[0] = DB;
    AirProperties[1] = WB;
    AirProperties[2] = HR;
    AirProperties[3] = RH;
    AirProperties[4] = DP;
    AirProperties[5] = SpVOL;
    AirProperties[6] = ENTH;

    FreeLibrary(MathRoutinesInstance);

    return AirProperties;
}

double DLL_EXPORT __cdecl Function8(double DBTemperature, double RelHumidity, double Pressure,int LoadLibraryFlag)   //
{
/*
    Input:
    DBTemperature - Dry bulb Temperature [C]
    RelHumidity - Relative Humidity [-]
    Pressure    - Barometric Pressure [kPa]

    Output:
    WBTemperature- Wet bulb Temperature [C]

    Reference:
    ASHRAE Fundamentals (SI) - Chapter 1
*/
  bool Converged;
  bool RHBounded;

  double WBTempHi;
  double WBTempLo;
  double WBTemperature;

  double RHCalc_Lo;

  double Params[2];

  Converged = false;

  WBTempHi = DBTemperature;
  WBTempLo = DBTemperature - 4.0f;

  HINSTANCE MathRoutinesInstance;

  if(LoadLibraryFlag == 0)
  {
    MathRoutinesInstance = LoadLibrary("./MathRoutines.dll");
    BisectionRootFinder = (fp_BisectionRootFinderTYPE) GetProcAddress(MathRoutinesInstance,"BisectionRootFinder");
  }

// Do some calculations here

  if(Converged == true)
    WBTemperature = WBTemperature;
  else
  {
    Params[0] = DBTemperature;
    Params[1] = Pressure;

    WBTemperature = BisectionRootFinder(WBTempLo,WBTempHi,RelHumidity,0.0005,Params,50,RelHumidityfn_WB_Params);
  }

  if(LoadLibrary == 0)
  {
      FreeLibrary(MathRoutinesInstance);
  }

  return WBTemperature;
}

如您所见,该dll引用了另一个dll“ MathRoutines.dll”来进行计算。 在Function8中,我有一个if块来加载Mathroutines dll。 这是因为我将在C#接口程序中使用dll,在该程序中将直接调用function8。 由于Function 8使用了MathRoutines.dll中的函数,因此需要在直接调用时加载它。

通过这种设置,程序遇到了我在原始帖子中提到的问题。 我意识到冻结与文件无关。 现在,作为一个简单的工作示例,我在Function8中注释了以下几行:

/*
  HINSTANCE MathRoutinesInstance;

  if(LoadLibraryFlag == 0)
  {
    MathRoutinesInstance = LoadLibrary("./MathRoutines.dll");
    BisectionRootFinder = (fp_BisectionRootFinderTYPE) GetProcAddress(MathRoutinesInstance,"BisectionRootFinder");
  }
*/

/*
  if(LoadLibrary == 0)
  {
      FreeLibrary(MathRoutinesInstance);
  }
*/

另外,我还必须在Function1的末尾评论FreeLibrary调用。 通过上述更改,该应用程序可以正常运行。 我似乎没有在这里包括FORTRAN代码,因为问题似乎与如何在C ++中加载MatRoutines库有关。

我想知道我上面显示的注释行是如何起作用的。 什么是加载/卸载DLL的正确方法。

我认为这是一个MSWindows程序。 我猜您对名称修改有疑问。 Fortran代码编译器在函数名称中添加一个下划线。 有时,您的编译器也会在下划线前加上下划线,因此您的函数BisectionRootFinder会变成BisectionRootFinder_或_BisectionRootFinder_

您应该看一下此命令的输出

dumpbin /SYMBOLS MathRoutines.dll

多年前,我使用了此例程。 现在,我没有MSWindows了,所以请不要完全信任命令语法,请尝试一下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM