简体   繁体   中英

Calling a Fortran function from Qt C++

I have this Fortran code.

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

The return value is a complex (complex in C++).

Here's the C++ code.

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.

I'm guessing that the crash has something to do with the return value of the Fortran function.

Any help?

Fortran subroutine

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

is equivalent to the C++ void function

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

(provided double is equivalent to real*8 , which is common)

The wrong signature used in the original code caused a mismatch and the runtime error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM