Минимальное воспроизведение код Python, который я пытаюсь написать, выглядит следующим образом:
Код: Выделить всё
import ctypes
from pathlib import Path
class Derived(ctypes.Structure):
...
library = ctypes.CDLL(str(Path(__file__).parent / "test_lib.so"))
n = 4
c_n = ctypes.c_int(n)
Derived._fields_ = [
("n", ctypes.c_int),
("y", ctypes.c_double * n)
]
d = Derived()
library.test_derived(ctypes.byref(c_n), ctypes.byref(d))
print(d.y[:])
Код: Выделить всё
module test_module
use iso_c_binding, only: c_double, c_int
implicit none
type, bind(c) :: Derived(n)
integer(c_int), len :: n
real(c_double), dimension(n) :: y
end type Derived
contains
subroutine test_derived(number, der) bind(c, name="test_derived")
integer(c_int), intent(in) :: number
type(Derived(n=number)), intent(out) :: der
der%y = 2.d0
write(*, *) "Fortran:", der%n, der%y
end subroutine test_derived
end module test_module
program main
use test_module
implicit none
integer, parameter :: n_points_1 = 5
integer, parameter :: n_points_2 = 6
type(Derived(n=n_points_1)) :: d1
type(Derived(n=n_points_2)) :: d2
call test_derived(n_points_1, d1)
call test_derived(n_points_2, d2)
end program
Подробнее здесь: https://stackoverflow.com/questions/791 ... ived-types