从Qt C++调用Fortran函数

Calling a Fortran function from Qt C++

本文关键字:Fortran 函数 调用 C++ Qt      更新时间:2023-10-16

我有这段Fortran代码。

subroutine DLL_TEST_PROJECT_001(dielconst, tandelta, kappa)
! Expose subroutine DLL_TEST_PROJECT_001 to users of this DLL
!
!DEC$ ATTRIBUTES DLLEXPORT::DLL_TEST_PROJECT_001
! Variables
implicit none
real*8::dielconst
real*8::tandelta
complex*16::kappa
! Body of DLL_TEST_PROJECT_001
kappa = dielconst * dcmplx(1.d0, tandelta)
end subroutine DLL_TEST_PROJECT_001

返回值是一个复数(C++中的复数)。

这是C++代码。

typedef complex<double> (*forTest)(double, double);
library.load("C:\forTest");
// The library loads ok.
forTest ft = (forTest)library.resolve("DLL_TEST_PROJECT_001");
// The function resolves ok and we have an address in ft.
// Now if I call the function...
complex<double> d = ft(1.0, 1.0);
// or just...
ft(1.0, 1.0);
// The app crashes with a segmentation fault.

我猜测崩溃与Fortran函数的返回值有关。

有什么帮助吗?

Fortran子程序

subroutine DLL_TEST_PROJECT_001(dielconst, tandelta, kappa)
  real*8 :: dielconst
  real*8 :: tandelta
  complex*16 :: kappa

相当于C++void函数

void c_name(double *dielconst, double *tandelta, complex <double> *kappa)

(假设double等同于real*8,这是常见的)

原始代码中使用的错误签名导致不匹配和运行时错误。