fortran-iso-c-binding

sockets programming gfortran

烂漫一生 提交于 2019-12-05 05:54:25
I want to be able to call networking functions in my Fortran application. My boss wants me to do everything in Fortran instead of using C and Fortran. We have already done a version of the application using PGI's Fortran compiler on Windows. We are moving it to Linux where we will probably use their compiler. Right now, I'm using gfortran. I have created an interface for these networking calls, and everything compiles and links. The code below is something similar to what I'm doing except the interfaces and constants are in a module. PROGRAM MAIN INTEGER,PARAMETER ::AF_INET = 2 INTEGER

iso_c_binding calling C routine with pointers from Fortran with arrays

╄→尐↘猪︶ㄣ 提交于 2019-12-04 16:31:53
I digged on the forum looking for a solution but I failed. My main problem is I am too noob with C language and Fortran-C interoperability in order to understand what I am doing wrong exactly. I want to call a C routine from Fortran but I'm having problem with the declaration of the variables. I made an example. This is the C routine: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <complex.h> #undef I int photon_trace(double x_init[4], double x_final[4]) //*************************************************** { double r,m,t,phi; t = x_init[0]; r = x_init[1]; m = x_init[2]; phi

Fortran-C interoperability and float arrays

走远了吗. 提交于 2019-12-03 20:50:05
I have a large existing Fortran95 code. It uses real(dp), dimension(num) :: array to declare arrays. I want to join in some C code and found that I can do this by writing interfaces to the C-functions and declaring arrays as use iso_c_binding real(c_double), allocatable, target :: array(:) I have working fortran functions which call the C-functions as call myfunction(c_loc(array)); What is needed to pass the real(dp) array to myfunction? Apparently, I would need to make a C-pointer from it (how?). Is there any other way than copying the array? Is it possible to ensure that both types indeed

How to access (dynamically allocated) Fortran arrays in C

倖福魔咒の 提交于 2019-12-03 04:00:49
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"); void (*fillArray)(double*); *(void **)(&fillArray) = dlsym(bar, "__bar_MOD_fillarray"); void (

Calling a C function from Fortran where the C function name was originally passed in from C

☆樱花仙子☆ 提交于 2019-12-02 02:41:02
For reasons that are not relevant, I need to pass a C/C++ function name into a Fortran subroutine, which, in turn, calls that C function. What I have found is that I can succesfully pass the function name into the Fortran subroutine. In that subroutine I can call the correct C function. However, the arguments of the C function get broken on this call (when called directly from C it works fine). I have used ISO C Binding to try and get this to work, to no avail. Here is a MWE: fortranRoutine.h: extern "C" { void fortranRoutine_(void(int status)); }; calledfromFortran.h: void calledfromFortran

Fortran 'parameter' type not included in compiled object

∥☆過路亽.° 提交于 2019-12-01 18:42:17
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 0000000000000000 T testmod._ 0000000000000000 D testmod_mp_thisissaved_ only the thisIsSaved variable is

Return string from Fortran to C++

▼魔方 西西 提交于 2019-12-01 04:05:29
问题 I have the following function call in C++: int strLength = 20; char* name; getName(name, strLength); printf("name: %s\n", name); and in Fortran: subroutine getName(name) bind (c, name='GETNAME') use,intrinsic :: iso_c_binding implicit none character, intent(out) :: name name = 'Martin' end subroutine getName When I execute the C++ routine the output is: name: M . Now, I suppose this happens because character, intent(out) :: name declares the name variable with size of 1, but if I change the

C equivalent to Fortran namelist

这一生的挚爱 提交于 2019-12-01 03:56:07
I am used to Fortran in which I used the namelist sequential read in to get variables out of a file. This allows me to have a file which looks like this &inputDataList n = 1000.0 ! This is the first variable m = 1e3 ! Second l = -2 ! Last variable / where I can name the variable by it's name and assign a value as well as comment afterwards to state what the variable actually is. The loading is done extremely easy by namelist /inputDataList/ n, m, l open( 100, file = 'input.txt' ) read( unit = 100, nml = inputDataList ) close( 100 ) Now my question is, is there any similar thing in C? Or would

C equivalent to Fortran namelist

北城余情 提交于 2019-12-01 00:34:47
问题 I am used to Fortran in which I used the namelist sequential read in to get variables out of a file. This allows me to have a file which looks like this &inputDataList n = 1000.0 ! This is the first variable m = 1e3 ! Second l = -2 ! Last variable / where I can name the variable by it's name and assign a value as well as comment afterwards to state what the variable actually is. The loading is done extremely easy by namelist /inputDataList/ n, m, l open( 100, file = 'input.txt' ) read( unit =

Intercepting Fortran STOP from C++

[亡魂溺海] 提交于 2019-11-30 15:44:01
问题 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.