C++ calling f90 function returning array
From: ujs (ujs31415_at_yahoo.com)
Date: 01/30/04
- Next message: Agis: "Harwell Subroutine Library MC29AD"
- Previous message: Tim Prince: "Re: fortran and columns"
- Next in thread: Jugoslav Dujic: "Re: C++ calling f90 function returning array"
- Reply: Jugoslav Dujic: "Re: C++ calling f90 function returning array"
- Reply: Herman D. Knoble: "Re: C++ calling f90 function returning array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 30 Jan 2004 06:27:54 -0800
Hello,
I am trying to call a function (in f90) returning arrays from C++.
How do I do it? (Using Visual C++/Fortran 6.6c).
It would be easier if the f90 was coded as a subroutine vice function, but I
would rather not modify an older routine.
I was able to write an f90 subroutine gateway that called the function and
returned the array in the argument. That worked fine.
But, I would like to call the original function (which was written as a
function to make it a pure function).
Here is a simplified example:
--------------------------
f90 function to be called
--------------------------
module temp ! takes care of the explicit interface
contains
function test( x ) result( y )
real(8), dimension(3) :: x
real(8), dimension(3) :: y
y = x
return
end function test
end module temp
----------------------------------
C++ caller:
-----------------------------------
#include <iostream>
// Prototype for TEST: note test is actually coded as a subroutine
extern "C" void __stdcall TEST( double* b, double* a );
int main()
{
double a[3] = {1,2,3};
double b[3];
TEST( b, a);
cout << "b = (" << b[0] << ", " << b[1] << ", " << b[2] << ");" << endl;
return 0;
}
-----------------------------------
f90 caller:
-----------------------------------
program f90_test
using temp
real(8) :: c(3) = (/ 1, 2, 3 /)
real(8) :: d(3)
d = test(c)
write(6,*) "d = ", d
end program f90_test
---------------------------------------------------------------
The problem is: (after working out all the linkage problems):
1. I noticed that the function returning array is actually coded like
"subroutine test( y, x )", hence the declaration in
C++ as "void( double*, double* )"
2. When extern "C" void __stdcall TEST( double* b, double* a );
I step into the f90 routine (with the debugger), the values
of x are correct (i.e., equal to a). However, y has location 0;
so writing to it causes the program to crash.
3. But, works fine when called from f90. Interestingly, the location of y
is different from location of d. When it returns from test, however,
the values for d are correctly set. Is this set by the caller or the
callee? I don't know what goes on the stack, and who clears stuff from
the stack.
So, how do I word the C++ declaration?
Thanks for any help!
ujs
- Next message: Agis: "Harwell Subroutine Library MC29AD"
- Previous message: Tim Prince: "Re: fortran and columns"
- Next in thread: Jugoslav Dujic: "Re: C++ calling f90 function returning array"
- Reply: Jugoslav Dujic: "Re: C++ calling f90 function returning array"
- Reply: Herman D. Knoble: "Re: C++ calling f90 function returning array"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|