c++调用Fortran 77

print_hi.for

      subroutine print_hi(n1, n2) bind(C)
          implicit none
          double precision n1(5)
          integer n2
          write(*, *) "Double precision array from Fortran: ", n1
          write(*, *) "An integer form Fortran: ", n2
          return
      end

test.cpp

#include <iostream>
extern "C" void print_hi(double *, int *);
using namespace std;

int main()
{
    double a[5] = {1., 2., 3., 4., 5.};
    int b = 1024;
    print_hi(a, &b);
    cout << "Hello from C++." << endl;
    return 0; 
}

编译

gfortran .\print_hi.for -c -fpic
gfortran .\print_hi.o -shared -o libprint_hi.dll
g++ .\test.cpp .\libprint_hi.dll -o test

运行

.\test

检查dll文件

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\dumpbin.exe" /exports "C:\Users\Linwei\Desktop\dll_test\libprint_hi.dll"
上一篇:python – f2py函数发布GIL


下一篇:使用ctypes在Python中使用Fortran可选参数