fortran-iso-c-binding

Passing allocated C_PTR to Fortran array to C

做~自己de王妃 提交于 2020-01-04 09:31:14
问题 I'm having trouble with segfaults from accessing an array in C, which is allocated in the Fortran file below. There are a few artefacts of debugging, such as the fact that the file writes don't write anything meaningful and I initialise a variable i that I never use. However, I've found the following: Not initialising i (but still declaring it): no segfault Not opening the file in C: no segfault Not printing HESS (not HESS_COPY ) elsewhere in the code: no segfault Declaring and initialising i

SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?

妖精的绣舞 提交于 2020-01-03 17:49:17
问题 Normally, the SAVE attribute is used in Fortran type declarations so that the variable retains its value at the end of a subprogram, such as described by the answers to the SO question here. However, I recently gave an example at another question for how a Fortran function can be written that returns only the C address of an allocatable character string constant to a C calling program, with the C_LOC intrinsic and other ISO_C_BINDING features of F2003. Should the SAVE attribute be used on the

c-fortran interoperability - derived types with pointers

ぐ巨炮叔叔 提交于 2020-01-02 15:26:30
问题 I have long fortran code that has to be made usable from python. I decided to do Fortran->C->Python interface. I got a problem: I have derived types within Fortran modules that contain double precision, allocatable type as members. When trying to compile with ifort I get (with gfortran something similar): Each component of a derived type with the BIND attribute shall be a nonpointer, nonallocatable data component with interoperable type and type parameters This is actually with agreement with

with c_f_pointer is fortran array reshape in-place or not

时光毁灭记忆、已成空白 提交于 2020-01-02 09:11:56
问题 I have a question related to one asked some years ago on Intel Developer Forum about the in-place reshaping of an array. In short, the answer was that an array of a certain rank can be allocated, and a pointer created that refers to the same memory location (i.e. in-place), but with a different rank, e.g.: use, intrinsic :: ISO_C_BINDING integer, allocatable, target :: rank1_array(:) integer, pointer :: rank3_array(:,:,:) integer :: i ! Allocate rank1_array allocate(rank1_array(24)) ! Created

How to pass arrays of strings from both C and Fortran to Fortran?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-02 04:27:05
问题 I am trying to pass an array of strings from C to a Fortran subroutine as well as from Fortran to that same Fortran subroutine. I have managed to pass single strings (i.e. 1D character arrays) successfully from both C and Fortran. However, I'm having trouble with arrays of strings. I am using ISO C binding on the Fortran side, and ideally I'd like this to be as seamless as possible on the calling side. I have read some related questions and answers. Some, (i.e. this and this) are simply "Use

Fortran 'parameter' type not included in compiled object

放肆的年华 提交于 2019-12-31 00:43:25
问题 I have a Fortran module that contains some variables that have the attribute parameter and some have the attribute save . The parameter ones are not included in the compiled object, which becomes a problem when trying to assemble a library. For example, consider a file testModule.f90 : module testMOD integer, save :: thisIsSaved = 1 integer, parameter :: thisIsParametered = 2 end module testMOD I compile this with: ifort -c testModule.f90 . When I check what's inside it: >$ nm testModule.o

Passing Fortran integer array to C subroutine only first element passed

柔情痞子 提交于 2019-12-25 06:53:09
问题 I am trying to pass an integer array from Fortran to C but I can only pass the first element of the array. I have the test program below which reproduces the error. Where am I going wrong? program test use foo integer (kind=c_int), allocatable :: hgmu_dose(:) allocate (hgmu_dose(0:10)) HGMU_dose(0)=22 HGMU_dose(1)=2 HGMU_dose(2)=3 HGMU_dose(3)=4 HGMU_dose(4)=5 HGMU_dose(5)=6 HGMU_dose(6)=7 HGMU_dose(7)=8 HGMU_dose(8)=9 HGMU_dose(9)=10 HGMU_dose(10)=11 print *, "HGMU_dose=", hgmu_dose call

Fortran functions with C interface result in undefined references when they try to call each other, why?

三世轮回 提交于 2019-12-24 12:14:04
问题 I have an old fortran code, for various reasons I am modifying it to provide functionality using c++. In this code there are two functions, CALC and CALC2. These functions need to be called from the c++ and they call each other. A sketch of CALC: SUBROUTINE CALC(W,NW,DW,IDMX,JUMP,POINT) bind(C, name="CALC") use iso_c_binding C Some statements IF (LO(36)) CALL CALC2(W,NW,DW,IDMX, .TRUE., 14) C More statements END A sketch of CALC2: SUBROUTINE CALC2(W,NW,DW,IDMX,JUMP,POINT) bind(C, name="CALC2"

fortran77, iso_c_binding and c string

浪尽此生 提交于 2019-12-24 07:59:01
问题 I am trying to call some Fortran77 code from C but I didn't find the proper way of passing a C char array. SUBROUTINE My_F_Code (c_message) BIND(C, NAME='my_f_code') USE ISO_C_BINDING IMPLICIT NONE CHARACTER*(C_CHAR) c_message CHARACTER*(256) f_message CALL C_F_POINTER( C_LOC(c_message), f_message) WRITE(*,*) f_message,LEN(f_message) END This method works with Fortran 90 and target, pointer specifier but Fortran 77 doesn't seems to have such things. So, the code above doesn't compile. The

Passing Fortran arrays to C

笑着哭i 提交于 2019-12-23 02:02:55
问题 I am having a lot of trouble passing Fortran arrays to a C program. From what I have gathered from previous posts is the inclusion of the interface. That got rid of some of my problems. However, I cannot seem to figure out how to pass these arrays properly or access their values correctly inside C. program f_call_c implicit none interface subroutine cfun(x,len) bind( c ) use,intrinsic :: iso_c_binding implicit none integer( c_int) :: len real(c_double) :: x(0:3,0:len) end subroutine cfun