fortran-iso-c-binding

Intercepting Fortran STOP from C++

…衆ロ難τιáo~ 提交于 2019-11-30 15:28:52
I prepared a C++ interface to a legacy Fortran library. Some subroutines in the legacy library follow an ugly but usable status code convention to report errors, and I use such status codes to throw a readable exception from my C++ code: it works great. On the other hand, sometimes the legacy library calls STOP (which terminates the program). And it often does it even though the condition is recoverable. I would like to capture this STOP from within C++, and so far I have been unsuccessful. The following code is simple, but exactly represents the problem at hand: The Fortran legacy library

Arrays of strings in Fortran-C bridges using iso_c_binding

只谈情不闲聊 提交于 2019-11-29 13:33:39
I'm writing code that will call a C function from Fortran using Fortran's C interoperability mechanism (introduced in Fortran 2003 and implemented in newer versions of gfortran and ifort). This answer is almost what I need, but I can't quite get my head around what interface declaration I should use in Fortran for a C function that looks like this: int use_array(int n, char * array[]){ int i; for(i=0; i<n; i++){ printf("Item %d = %s\n",i,array[i]); } return n; } I'm not clear what the declaration should be for the interface on the Fortran end: interface function use_array(n, x) bind(C) use iso

ISO_C_BINDING Calling C routine from Fortran (with doubles and arrays)

谁都会走 提交于 2019-11-29 12:53:34
I posted a similar question few weeks ago ( iso_c_binding calling C routine with pointers from Fortran with arrays ) and I found a solution to my problem. Now I modified few things and I am having some problems again. In the following a simplified version of my problem. I have a main program in fortran: program main_dummy ! compile: gcc -c dummy_trace.c ! f95 raytracing.f90 main_dummy.f90 dummy_trace.o -o main use, intrinsic :: ISO_C_BINDING use raytracing implicit none !real(kind=8) :: x_in(4), x_fin(4) real(C_DOUBLE), dimension(0:3) :: x_in, x_fin real(C_DOUBLE) :: spin integer :: rt_ok x_in

call functions from a shared fortran library in python

对着背影说爱祢 提交于 2019-11-28 18:06:32
问题 I would like to call some functions from a Fortran shared library in Python. I have found some links on the net and read them, and according what I found, I should do libadd = cdll.LoadLibrary('./libbin.so') to load the shared object. However, this shared object includes some symbols from another shared library. I read the help of cdll however it does not seem possible to load several shared object files at the same time. How may I call functions from this Fortran library, which is most

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

强颜欢笑 提交于 2019-11-28 14:18:30
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 be circumstances when double precision will not be the same as C_DOUBLE . A real literal may be

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

吃可爱长大的小学妹 提交于 2019-11-28 14:14:52
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 *eind, idx_t *vwgt, idx_t *vsize, idx_t *nparts, real_t *tpwgts, idx_t *options, idx_t *objval, idx_t

pass fortran 77 function to C/C++

核能气质少年 提交于 2019-11-28 13:54:29
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 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 2003, and while there are extremely few Fortran compilers that fully support the entirety of Fortran 2003, the

Passing a two dimentional array from Fortran to C

左心房为你撑大大i 提交于 2019-11-28 13:03:13
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,2)=2 a(1,3)=3 a(2,1)=4 a(2,2)=5 a(2,3)=6 a(3,1)=7 a(3,2)=8 a(3,3)=9 interface subroutine print2(a,n)

Unable to pass array from FORTRAN to C

我与影子孤独终老i 提交于 2019-11-28 11:19:49
问题 I am trying to pass a single dimension array from a FORTRAN program to C. The C function is called but the values that it holds are garbage. But whereas if I try calling the same function using an integer variable I am able to pass the required value. Can anyone help me out with this? The code I am using is similar to this File: fortran_prog.f program test real*4 :: a(4) data a / 1,2,3,4 / call test_func(a) end program test File: c_prog.c int test_func(double a[]) { int i; for(i=0;i<4;i++) {

Calling C function/subroutine in Fortran code

﹥>﹥吖頭↗ 提交于 2019-11-28 11:19:33
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: Microsoft (R) Incremental Linker Version 8.00.50727.762 Copyright (C) Microsoft Corporation. All rights