fortran-iso-c-binding

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

你。 提交于 2019-11-27 06:12:37
问题 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

Calling C function/subroutine in Fortran code

僤鯓⒐⒋嵵緔 提交于 2019-11-27 06:07:06
问题 I am attempting to compile and link a Fortran code calling c subroutine: Fortran code: program adder integer a,b a=1 b=2 call addnums(a,b) stop end program C code: void addnums( int* a, int* b ) { int c = (*a) + (*b); /* convert pointers to values, then add them */ printf("sum of %i and %i is %i\n", (*a), (*b), c ); } I used the following commands to compile and link in windows environment. ifort -c adder.f cl -c addnums.c ifort -o add adder.obj addnums.obj I get the following error:

Calling Fortran subroutines with optional arguments from C++

妖精的绣舞 提交于 2019-11-27 03:26:01
问题 How would I reference a Fortran function in a C++ header that uses optional arguments? Would I have a prototype in the header for each possible combination of calls? Or is this even possible? For instance, Fortran: subroutine foo(a, b, c) bind(c) real, intent(in), optional :: a, b, c ... end subroutine foo 回答1: It is not possible, at least portably, unless you make the subroutine bind(C) . Once you make it bind(C) , it is just passing of a pointer which can be NULL on the C side. subroutine

Fortran derived types containing pointers to be accessible from C

喜你入骨 提交于 2019-11-26 21:58:57
问题 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

Fortran - Cython Workflow

六眼飞鱼酱① 提交于 2019-11-26 19:09:39
问题 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

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

二次信任 提交于 2019-11-26 18:35:23
问题 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) ! !!!!!!!!!!!!!!!!!!!!!!

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

隐身守侯 提交于 2019-11-26 17:42:50
问题 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

Calling a FORTRAN subroutine from C

萝らか妹 提交于 2019-11-26 07:35:44
问题 I am trying to call a FORTRAN function from C My questions are: If fortRoutine is the name of my fortran subroutine, then I am calling this from C as fortRoutine_ . If fortRoutine contains only one character array argument, then can I pass like this: fortRoutine_(\"I am in fortran\"); While calling FORTRAN subroutines, when should I use pass by value and when pass by reference? As I am new to C, I do not have a clue about this. If possible, please suggest some good tutorial links as well. 回答1