K
Also ich nochmal,
ich verstehe es einfach nicht, wenn ich jetzt Arrays übergeben möchte, wie z.B. in diesem Minifortranprogramm:
program main
use foo_module
implicit none
integer, parameter :: n = 4
real(8), dimension(1:n) :: x, y
type(foo_type) :: foo
x = (/ 1,2,3,4 /)
y = (/ 1,4,9,16 /)
call new(foo)
call show(foo,x,y,n)
call delete(foo)
end program main
Das zugehörige foo_module sieht wie folgt aus und dabei klappt die Übergabe nicht. Ich verstehe das so, dass in den Zeilen 37 bis 51 seitens Fortran definiert wird, was die subroutinen zu erwarten haben und darüber Zeile 9 bis 24 findet die Konvertierung nach C statt?
module foo_module
use, intrinsic :: ISO_C_BINDING, only: C_int, C_ptr, C_NULL_ptr
implicit none
private
type foo_type
private
type(C_ptr) :: object = C_NULL_ptr
end type foo_type
interface
function C_foo__new() result(this) bind(C,name="foo__new")
import
type(C_ptr) :: this
end function C_foo__new
subroutine C_foo__delete(this) bind(C,name="foo__delete")
import
type(C_ptr), value :: this
end subroutine C_foo__delete
subroutine C_foo__show(this,x,y,n) bind(C,name="foo__show")
import
type(C_ptr), value :: this
integer(C_int), value :: n
type(C_ptr), value :: x,y
end subroutine C_foo__show
end interface
interface new
module procedure foo__new
end interface new
interface delete
module procedure foo__delete
end interface delete
interface show
module procedure foo__show
end interface show
public :: new, delete, show, foo_type
contains
! Fortran wrapper routines to interface C wrappers
subroutine foo__new(this)
type(foo_type), intent(out) :: this
this%object = C_foo__new()
end subroutine foo__new
subroutine foo__delete(this)
type(foo_type), intent(inout) :: this
call C_foo__delete(this%object)
this%object = C_NULL_ptr
end subroutine foo__delete
subroutine foo__show(this,x,y,n)
type(foo_type), intent(in) :: this
integer, parameter :: n
real(8), dimension(1:n) :: x,y
call C_foo__show(this%object,x,y,n)
end subroutine foo__show
end module foo_module
Der zugehöirge C Wrapper sieht so aus:
#include <iostream>
#include <vector>
class foo
{
public:
void show(double*, double*, int);
private:
std::vector<double> _x, _y;
};
auto foo::show(double* x, double* y, int n) -> void
{
for(int i = 0; i < n; ++i)
{
_x.push_back( x[i] );
_y.push_back( y[i] );
}
for(unsigned int i = 0; i < _x.size(); ++i)
std::cout << _x[i] << "\t" << _y[i] << std::endl;
}
/* C wrapper interfaces to C++ routines */
extern "C" {
foo *foo__new(){
return new foo();
}
void foo__show(foo *This, double* x, double* y, int n){
This->show(x,y,n);
}
void foo__delete(foo *This){
delete This;
}
}
Kann mir jemand trotz C++ Forum bei der Sache mit Fortran weiterhelfen?
Gruß,
-- Klaus.