DanielS
DanielS

Reputation: 75

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?

Upvotes: 2

Views: 434

Answers (1)

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.

Upvotes: 3

Related Questions