fortran-iso-c-binding

Passing a dynamic 2D array from C++ to Fortran and back

て烟熏妆下的殇ゞ 提交于 2019-12-10 11:15:09
问题 Passing a fixed 2D array between C++ and Fortran works fine, however not so with the program I have written to pass a 2D dynamic array from C++ to Fortran. C++ side extern "C" {void array2d_(double **, int *, int *); } using namespace std; int main() { double **array; int nx=3; int ny=2; int i,j; cout << "Passing dynamic array from C to Fortran\n"; array = (double **) malloc(nx * sizeof(double *)); if(array == NULL) { fprintf(stderr, "out of memory\n"); exit; } for(i = 0; i < nx; i++) { array

How to prepare a fortran module from a C header file?

爱⌒轻易说出口 提交于 2019-12-10 11:13:57
问题 I have this pure C file with 6 functions that I want to make available for fortran programmers: http://tinyfiledialogs.sourceforge.net Could C to Fortran "Import" work ? Should I simply prepare something like a Fortran header file instead ? I realise that my C code is using several Unix or Windows C header files, and this will complicate the conversion. But surely offering the equivalent of a Fortran header for 6 C functions must be achievable. char const * tinyfd_inputBox ( char const *

How to access (dynamically allocated) Fortran arrays in C

五迷三道 提交于 2019-12-09 05:05:57
问题 My main question is why arrays do such weird things and whether there is any way at all to do the following in a "clean" way. I currently have a C program foo.c interfacing a Fortran program bar.f90 via dlopen/dlsym , roughly like in the code below: foo.c: #include <dlfcn.h> #include <stdio.h> int main() { int i, k = 4; double arr[k]; char * e; void * bar = dlopen("Code/Test/bar.so", RTLD_NOW | RTLD_LOCAL); void (*allocArray)(int*); *(void **)(&allocArray) = dlsym(bar, "__bar_MOD_allocarray")

Fortran derived types containing derived types to be accessible from C

喜夏-厌秋 提交于 2019-12-09 03:51:27
As an extension to this post , I have derived types which have as members derived types themselves. Example below: module simple use iso_c_binding TYPE SIMPLEF INTEGER :: A INTEGER, POINTER :: B, C(:) END TYPE SIMPLEF TYPE COMPLEXF INTEGER :: X TYPE (SIMPLEF) :: Y END TYPE COMPLEXF end module simple The aim is, as in the post above, to have similar derived types in C and to be able to pass values back and forth to Fortran. The solution can be seen here . However here it's not just a derived type, it is a derived type with a member which is a derived type itself. Would I need to create for

Passing Fortran arrays to C

雨燕双飞 提交于 2019-12-08 18:43:30
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 subroutine vec(r,len) bind(c) use,intrinsic :: iso_c_binding implicit none integer(c_int) :: len real(c

Passing a FORTRAN object to C and vice versa

我是研究僧i 提交于 2019-12-08 05:34:08
问题 I have my Fortran object i.e. this%object%a this%object%b this%object%c I want to pass it to a code written in C, I am predominately a FORTRAN programmer and I have had very little exposure to C. I am using iso_c_binding to pass integers and arrays but now I need to pass objects. I define the object in the following way TYPE object INTEGER :: a INTEGER :: b INTEGER :: c END TYPE object 回答1: You can make interoperable types: use iso_c_binding TYPE, BIND(C) :: object INTEGER(c_int) :: a INTEGER

Using a C++ class object in fortran 77

时间秒杀一切 提交于 2019-12-08 04:50:15
问题 Is there a way to pass a C++ object to use with Fortran 77? For example: C23456 program main write (*,*) 'Hello from FORTRAN 77!' call readstep('cube.stp'//CHAR(0),myshape) stop end and then use the myshape as a C++ object which will just be kept in memory used by Fortran and to just pass it to other C++ functions that will actually use it? EDIT: Here is the C++ code: extern"C" { void readstep_(char*,void*); } void readstep_(char* inputFile, void* outShape){ STEPControl_Reader reader; reader

Passing pointer from C to fortran Subroutine

微笑、不失礼 提交于 2019-12-08 01:59:39
问题 I am trying to call a fortran subroutine from C, can I allocate in C and pass the pointer to Fortran safely? The array in the subroutine is an automatic array (x(nmax)). (I am allocating the x and then passing it to the fortran) 回答1: Yes. Modern Fortran guarantees that Fortran routines can be called from C and vice-a-versa. This is done via the Fortran ISO_C_BINDING. This is part of Fortran 2003 and was widely available as an extension to Fortran 95 compilers. There is documentation in the

Use Fortran-code in C

不羁岁月 提交于 2019-12-07 13:31:01
问题 I try to use a fortran-routine in C, but I doesn't work. I don't know where I made a mistake. Here my Fortran-code including the Integration-Module, which I want to use in C: module integration implicit none contains function Integrate(func, a,b, intsteps) result(integral) interface real function func(x) real, intent(in) :: x end function func end interface real :: integral, a, b integer :: intsteps intent(in) :: a, b, intsteps optional :: intsteps real :: x, dx integer :: i,n integer,

What is the official way to deal with string in C++/FORTRAN interop

时光怂恿深爱的人放手 提交于 2019-12-07 07:08:47
问题 I'd like to learn the latest improvement in C++/FORTRAN interoperability when it comes to string in particular. The following is my unsuccessful attempt, please help me correct or advise a better solution. My compiler is gcc 4.8.5 In C++ #include <iostream> extern "C"{ void SayHello(char*); } int main(int argc, char** argv){ char * name = argv[1]; SayHello(name); return 0; } In Fortran module MyModule contains subroutine SayHello(people) bind(c,name="SayHello") use, intrinsic :: iso_c_binding