fortran-iso-c-binding

Pass arrays from C/C++ to Fortran and return a calculated array

一曲冷凌霜 提交于 2019-11-28 11:19:16
I am trying to pass an array from C/C++ into a Fortran 2003 module and get the calculated values back into C/C++. I've been able to pass and return single values (scalars) just fine, but getting an array back and forth is proving difficult. I've found many threads on scalar values and I've been successful at making those work. I've modeled my array based functions after my working scalar functions. I am using gcc/gfortran. Here's the Fortran module (ConvertUnitsLib.f03). module ConvertUnitsLib use :: iso_c_binding ! for C/C++ interop real(c_double), bind(c) :: degF, degC public DegCtoF

How to allocate an array inside fortran routine “called” from C

本小妞迷上赌 提交于 2019-11-28 08:20:16
问题 I think title says what I need. I know we can use "asd" function to do this, but for some reasons I need to do the allocation in Fortran (i.e. in subroutine "asd_"). Here is the C code: #include <stdio.h> void asd(float **c) { *c = (float *) malloc (2*sizeof(float)); **c =123; *(*c+1)=1234; } void asd_(float **c); main () { float *c; asd_(&c); // asd(&c); would do the job perfectly printf("%f %f \n",c[0],c[1]); return 0; } And here is the Fortran code: subroutine asd(c) implicit none real,

Fortran derived types containing pointers to be accessible from C

喜夏-厌秋 提交于 2019-11-28 01:30:03
I have a Fortran code with many derived types containing pointers. I am writing a C++ code which needs to access these variables. I cannot rewrite these derived types without the pointers as they are used in hundreds of different places all over the Fortran code. Below is a sample code: module simple use iso_c_binding TYPE,bind(C) :: SIMPLEF INTEGER :: A INTEGER, POINTER :: B, C(:) END TYPE SIMPLEF end module simple I need to access the SIMPLEF derived type from C. I know I cannot use it as it is, since Fortran pointers cannot be in a derived type if that is supposed to be accessible from C.

Using scientific notation and underscore kind specifier at the same time for real literals in Fortran

二次信任 提交于 2019-11-27 19:33:59
问题 Using scientific notation for floating point literals is easy enough in Fortran: 1.5d-10 would mean a double precision (whatever that means under current Fortran compiler settings) floating point value that approximates 1.5*10^-15 . However, the fusion of the exponent notation and the floating point kind specifier is a bit of an issue. How would one declare this floating point literal when one wants it to have a type of C_DOUBLE ? I know that this is a bit of a nitpicking issue, but there can

Fortran - Cython Workflow

本小妞迷上赌 提交于 2019-11-27 17:44:52
I would like to set up a workflow to reach fortran routines from Python using Cython on a Windows Machine after some searching I found : http://www.fortran90.org/src/best-practices.html#interfacing-with-c and https://stackoverflow.com/tags/fortran-iso-c-binding/info and some code pices: Fortran side: pygfunc.h: void c_gfunc(double x, int n, int m, double *a, double *b, double *c); pygfunc.f90 module gfunc1_interface use iso_c_binding use gfunc_module implicit none contains subroutine c_gfunc(x, n, m, a, b, c) bind(c) real(C_FLOAT), intent(in), value :: x integer(C_INT), intent(in), value :: n,

How to debug Fortran 90 compile error “There is no specific subroutine for the generic 'foo' at (1)”?

纵饮孤独 提交于 2019-11-27 16:22:09
I am trying to write Fortran 2003 bindings to CUFFT library using iso_c_bindings module, but I have problems with cufftPlanMany subroutine (similar to sfftw_plan_many_dft in FFTW library). The bindings itself look like this: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! ! cufftResult cufftPlanMany(cufftHandle *plan, int rank, int *n, ! int *inembed, int istride, int idist, ! int *onembed, int ostride, int odist, ! cufftType type, int batch) ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! interface cufftPlanMany subroutine cufftPlanMany(plan

Calling METIS API(wrtten in C language) in fortran program

人盡茶涼 提交于 2019-11-27 08:31:49
问题 Over 2 weeks, I've struggled to call one of the METIS library written in C from my fortran code. And, unfortunately, It doesn't seem to be a HAPPY END without your help. I found some posts about direct calling and using interface. I prefer the latter because I could monitor the variables for debugging. There are three codes I attached. 1. c function I'd like to use 2. fortran interface module 3. fortran program (1) c function int METIS_PartMeshNodal(idx_t *ne, idx_t *nn, idx_t *eptr, idx_t

pass fortran 77 function to C/C++

北城余情 提交于 2019-11-27 07:55:53
问题 Is it possible to pass fortran 77 function as a callback function pointer to C/C++? if so, how? information I found on the web relates to fortran 90 and above, but my legacy code base is in 77. many thanks 回答1: If it can be done in FORTRAN 77, it will be compiler and platform specific. The new ISO C Binding of Fortran 2003 provides a standard way of mixing Fortran and C, and any language that follows or can follow the calling conventions of C, such as C++. While formally a part of Fortran

Creating a FORTRAN interface to a C function that returns a char*

时间秒杀一切 提交于 2019-11-27 07:54:17
I've been held up on this for about a week, now, and have searched forum after forum for a clear explanation of how to send a char* from C to FORTRAN. To make the matter more frustrating, sending a char* argument from FORTRAN to C was straight-forward... Sending a char* argument from FORTRAN to C (this works fine): // The C header declaration (using __cdecl in a def file): extern "C" double GetLoggingValue(char* name); And from FORTRAN: ! The FORTRAN interface: INTERFACE REAL(8) FUNCTION GetLoggingValue [C, ALIAS: '_GetLoggingValue'] (name) USE ISO_C_BINDING CHARACTER(LEN=1, KIND=C_CHAR),

Passing a two dimentional array from Fortran to C

前提是你 提交于 2019-11-27 07:30:51
问题 I am having trouble passing a two dimensional array from Fortran to C. The following is my C function which just displays the array elements on the screen. #include <stdio.h> void print2(double *arr , int *n) { int y = *n; printf("\n y = %d", y); for(int i =0; i<y; i++) { for (int j = 0; j < y; j++) printf("%.6g", *((arr + i*y) + j)); printf("\n"); } } My Fortran code so far is the following: program linkFwithC use, intrinsic :: iso_c_binding implicit none real, dimension(3,3)::a a(1,1)=1 a(1