Fortran与C++交叉编程:Fortran如何调用C++动态库踩坑(Fortran-er的生存指南)

Fortran程序中调用C++编写的动态库的问题分为两个子问题

  • Fortran调用c形式函数接口
  • 为c++动态库编写c形式的函数接口

下面把踩坑过程进行分享

1. C++类库编写C形式接口

1.1 c与c++的函数符号名

c标准的函数名与编译后的符号名一致(使用gcc编译的.c文件中的函数),但c++的函数名与编译后的符号名不一致(g++编译的.cpp文件中的函数)。未使c++的函数按照c标准编译,需要使用extern "C"关键字。

那么,在c++的类库中,若希望某些函数表现出于c标准相同的接口形式,那么在头文件中应按照下面格式书写。经过__cplusplus预编译指令的包装后,该头文件使其中声明的函数在c,c++的编译器中均按照c标准的函数形式表现。

// foo_and_bar.h

#ifdef __cplusplus
extern "C" {
#endif

void foo(int,);
void bar(double);

#ifdef __cplusplus
}
#endif

1.2 c++的类封装

在c中没有class的概念,为c++类库编写的c形式的函数接口中,不能直接使用class声明的类,那么需要为c++类声明的头文件中如下编写。

// moment.h
#ifndef MOMENT_H
#define MOMENT_H

#ifdef __cplusplus
class Moment
{
private:
    int jd;
    double js;
public:
    Moment(int jd, double jd);
    int get_jd() const;
    double get_js() const;
};
#else
typedef struct Moment
    Moment;
#endif

#ifdef __cplusplus
extern "C" {
#endif

extern void moment_new(Moment* obj, int jd, double js);

extern int moment_get_jd(const Moment* obj);

extern double moment_get_js(const Moment* obj);

#ifdef __cplusplus
}
#endif

#endif

如此声明的Moment头文件展示出了c与c++两种形式的特性,在c++编译器中Moment类型的定义是完整的,既可以使用c++类操作使用Moment,也可以使用与c公用的函数方法接口moment_new(),moment_get_jd(),moment_get_js()

不过在c编译器中,Moment仅是一个代号。仅可以按照moment_new(),moment_get_jd(),moment_get_js()函数的接口形式操作。因为在c编译器看来,Moment仅是一个代号,因此这三个函数中关于Moment的参数必须是指针形式参数。相关实现函数为

// moment.cpp
void moment_new(Moment* obj,  int jd, double js)
{
    obj= new Moment(jd, js);
}

int moment_get_jd(const Moment* obj) const
{
    return obj->get_jd();
}

double moment_get_js(const Moment* obj) const
{
    return obj->get_js();
}

2 Fortran调用c函数

2.1 Fortran调用函数规则

fortran编译器,如gfortran,基本相当于c编译器gcc的前端,经gfortran解析后的fortran程序最终还是按照c的逻辑编译。其中fortran的函数有一些特点:

  • fortran中函数名不区分大小写fooFooFOO是相同函数
  • fortran函数名对应的c函数名后面加下划线,即在fortran中调用了函数foo(),相当于在c中调用函数foo_()
  • fortran函数的参数均是按指针传递,即在fortran中调用函数foo(INTEGER*4, REAL*8)相当于在c中调用函数foo_(int*, double*)

2.2 将c++类库写作Fortran可调用形式

以上述Moment类,调整其中的c部分接口函数形式

// moment.h
#ifndef MOMENT_H
#define MOMENT_H

#ifdef __cplusplus
class Moment
{
private:
   int jd;
   double js;
public:
   Moment(int jd, double jd);
   int get_jd() const;
   double get_js() const;
};
#else
typedef struct Moment
   Moment;
#endif

#ifdef __cplusplus
extern "C" {
#endif

extern void moment_new_(Moment** obj, int* jd, double* js);

extern int moment_get_jd_(const Moment** obj);

extern double moment_get_js_(const Moment** obj);

#ifdef __cplusplus
}
#endif

#endif

主要的变化是,c形式接口函数moment_new(),moment_get_jd(),moment_get_js()中所有参数增加一级直至,关于Moment的参数修改为二级指针,即指针的指针,intdouble修改为int*double*;此外为每个函数名后增加下划线_。对应的实现为

// moment.cpp
void moment_new_(Moment** obj,  int* jd, double* js)
{
    *obj= new Moment(*jd, *js);
}

int moment_get_jd_(const Moment** obj) const
{
    return *obj->get_jd();
}

double moment_get_js_(const Moment** obj) const
{
    return *obj->get_js();
}

2.3 Fortran调用c接口形式的c++类库

有如下要点:

  • type(c_ptr) :: foo声明相当于c语言中声明foo的类型为void *
  • 使用type(c_ptr)需要use iso_c_binding语句
  • 调用c接口外部函数,需要用external关键字声明
  • 若要使用moment_get_jd()moment_get_js()函数返回值,必须先声明
program main
    use iso_c_binding
    implicit none

    type(c_ptr) :: foo

    external moment_new
    integer(4) moment_get_jd
    external moment_get_jd
    real(8) moment_get_js
    external moment_get_js
    integer(4) jd
    real(8) js

    call moment_new(foo, 1, 2d0)

    jd = moment_get_jd(foo)
    js = moment_get_js(foo)
    write(*,*) jd, js
end

以Fortran的call moment_new(foo, 1, 2d0)语句为例,其中foovoid*格式,12d0分别为INTEGER*4REAL*8的常量,那么该语句的调用相当于调用c函数moment_new_(void**, int*, double*),与我们为Moment类编写的接口函数extern void moment_new_(Moment** obj, int* jd, double* js)匹配。

3 平台兼容性至动态库加载失败

通常,动态库Windows下的.dllLinux下的.so文件找不到,加载失败是因为动态库路径设置错误导致。一、编译阶段,动态库路径直接使用-L{dynamic_library_path}-l{dynamic_library_name}两个选项指定;二、进程加载与运行阶段,动态库由链接器搜索寻找,Linux下可以设置环境变量LD_LIBRARY_PATH指定,Windows下可以将动态库至于exe文件相同文件夹下。

若c++类库与fortran编译器的平台版本不同,也会导致动态库文件找不到、加载失败问题。例如,新编写的c++类库使用的的64-bit编译组件编译的.dll文件,而fortran使用的是 32-bit的gfortran,那么在fortran编译时则找不到动态库文件,更进一步,找不到的是32-bit的C++动态库

你可能感兴趣的:(Fortran与C++交叉编程:Fortran如何调用C++动态库踩坑(Fortran-er的生存指南))