c++ - 混合编程 - 包括 C++ 头文件到 Fortran

标签 c++ fortran fortran-iso-c-binding

我正在尝试在用 Fortran 编写的程序中使用用 C++ 编写的库中的函数。 C++ 库总结在一个头文件中,因此如果您想在另一个 C++ 程序中使用它,您只需执行 #include functions.h 我想了解如何在 Fortran 中执行类似的操作。

根据我的研究,我创建了这个最小的可行示例:

clib/functions.h:

#ifndef ADD_H
#define ADD_H
extern "C"
{
int __stdcall add(int x, int y);
} 
#endif

clib/函数.cpp:

extern "C"
{
int __stdcall add(int x, int y)
{
    return x + y;
}
}

cinclude.c

#include "clib/functions.h"

cinterface.f95:

module cinterface
  use,intrinsic::ISO_C_BINDING
  integer(C_INT)::a,b
  interface
    integer(C_INT) function add(a,b) bind(C,name="add")
      use,intrinsic::ISO_C_BINDING
      implicit none
!GCC$ ATTRIBUTES STDCALL :: add
!DEC$ ATTRIBUTES STDCALL :: add
      integer(C_INT), value ::a,b
    end function add
  end interface
end module cinterface

主.f90

 program main
   use cinterface      
   implicit none
   integer :: c
   c = add(1,2)
   write(*,*) c
 end program

生成文件:

FC = gfortran
CC = g++
LD = gfortran
FFLAGS = -c -O2
CFLAGS = -c -O2
OBJ=main.o 
DEP = \
    cinterface.o cinclude.o

.SUFFIXES: .f90 .f95 .c .o
# default rule to make .o files from .f files
.f90.o  : ;       $(FC) $(FFLAGS) $*.f90 -o $*.o
.f95.o  : ;       $(FC) $(FFLAGS) $*.f95 -o $*.o
.c.o  : ;       $(CC) $(CFLAGS) $*.c -o $*.o
%.o: %.mod
#
main.ex: ${DEP} ${OBJ}
    $(LD)  ${DEP}  ${OBJ} -o prog.exe
#

当我尝试使用 Cygwin 制作此项目时,出现以下错误:

main.o:main.f90:(.text+0x13): undefined reference to `add'
main.o:main.f90:(.text+0x13): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `add'
collect2: error: ld returned 1 exit status
make: *** [makefile:19: main.ex] Error 1

如何在 Fortran 中使用 add 函数?

最佳答案

您已经完成了大部分工作。要使这项工作有效,您需要解决两件事:链接和参数传递约定。

联动

正如 francescalus 指出的那样,Fortran 编译器不理解如何解析 C/C++ 头文件。因此,您的 functions.h 和 cinclude.c 文件在此示例中不会有任何用处。

不过,请不要丢弃您的 functions.h。在其中您将添加函数声明为:

extern "C"
{
    int __stdcall add(int x, int y);
} 

extern "C" 是重要的部分。这告诉 g++ 以下代码块中的符号不​​受所有 C++ name mangling 的约束。 .您将需要围绕 functions.cpp 中的 add 定义。

extern "C"
{
    int add(int x, int y)
    {
        return x + y;
    }
}

完成后,您只需链接 functions.o、cinterface.o/mod 和 main.o。

参数传递约定

add 的声明方式是将参数xy 按值传递给函数。这是 C/C++ 函数参数的默认行为。另一方面,Fortran 默认通过引用将参数传递给函数/子例程。在 C++ 中,这看起来像 int add(int* x, int* y)。有两种方法可以解决这个问题。

第一个选项是使用参数的整数指针重新定义您的 add 函数,并在函数内部取消引用它们。

extern "C"
{
    int add(int* x, int* y)
    {
        return *x + *y;
    }
}

第二个选项(恕我直言,首选选项)是声明 Fortran 接口(interface)以按值传递参数。它们没有在 add 函数中被修改...为什么要通过引用传递它们?如果您选择此选项,那么您的 cinterface.f95 将需要包含以下 add 声明:

integer(C_INT) function add(a,b) bind(C,name="add")
    use,intrinsic::ISO_C_BINDING
    implicit none
    integer(C_INT),value::a,b
end function add

注意变量 ab 上额外的 value 装饰。无论您选择哪个选项,如果我的机器上没有它,我都会打印出 8393540 作为 add 函数调用的结果。在解决参数传递约定后,我按预期打印了 3。

关于c++ - 混合编程 - 包括 C++ 头文件到 Fortran,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51266724/

相关文章:

java - 简单的圆圈碰撞 libgdx

c++ - 我们是否应该创建具有固定大小的 vector ,然后在需要更多或不初始化时 push_back

arrays - 如何在 Fortran 中通过 BLAS 加速高阶张量收缩的 reshape ?

arrays - 如何将标量传递给 Fortran 子程序的向量(一维数组)?

compiler-errors - 如何在 (1)"处调试 Fortran 90 编译错误 "There is no specific subroutine for the generic ' foo'?

c++ - 更改已编译二进制文件的部分

fortran - 在 Fortran 中使用 CMPLX 函数实现 double 或四精度

可以使用数组参数从 Fortran 调用标量 C 函数吗?

c - 使用 Visual Studio 结合 Intel Fortran,如何解决使用不同类型的名称重整问题

c++ - 如何从被阻止的调用中返回?