从 Qt C++ 调用 Fortran 函数

Calling a Fortran function from Qt C++

我有这个 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

return 值是复数(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 函数的 return 值有关。

有什么帮助吗?

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,这是常见的)

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