我面临的问题如下:
我从一个 f90 文件生成了一个名为 customkinetics.so 的共享对象,我在一个用 C++ 编写的带有 python 接口的 Cantera(化学代码)中使用了这个对象。该对象在子例程 (CustomKinetics.cpp) 中调用,如下所示:
#include "cantera/kinetics/CustomKinetics.h"
#include <iostream>
#include <dlfcn.h>
using namespace std;
namespace Cantera
{
//Fortran External Routine
extern "C"
{
void customkinetics_(doublereal* P, doublereal* T, doublereal* rho, const doublereal* m_y, doublereal* wdot);
}
CustomKinetics::CustomKinetics(thermo_t* th) : GasKinetics(th)
{
printf("WARNING: Using customized kinetics from f90 file.\n");
// Closing the library if it has already been opened
if (handle == NULL){
dlclose(handle);
}
handle = dlopen("customkinetics.so", RTLD_LAZY | RTLD_LOCAL);
// load symbol
ck = (ck_t) dlsym(handle, "customkinetics_");
}
void CustomKinetics::get_wdot_custom(doublereal* wdot)
{
doublereal P = thermo().pressure();
doublereal T = thermo().temperature();
doublereal rho = thermo().density();
const doublereal* m_y = thermo().massFractions();
// calculation
ck(&P,&T,&rho,&m_y[0],&wdot[0]);
}
}
它工作正常,直到我覆盖 .f90 文件,然后我再次编译,从而覆盖 .so ,然后当我再次调用它时,我实际上调用了第一个对象。
我知道 dlclose() 不应该从内存中完全删除对象,但是有什么办法吗?
fortran Makefile:
customkinetics.so: customkinetics.f90
gfortran -c customkinetics.f90 -g -fPIC -o customkinetics.o
gfortran -shared -o customkinetics.so customkinetics.o