Fortran and C interoperability: receive return value from c (calling from Fortran)

Spartan

I have read a lot of posts on a similar topic but I have not yet succeeded resolving this. I should mention that I have simplified my code a lot for this post. My intention is to use a c function by calling it from fortran77 and receiving back values from c. The fact that I mention fortran77 is because I want to link my code to a much larger project that uses fortran77, but I am willing to consider solutions with other versions of fortran if they do the job and if you believe they will simplify my problem.

I have two files: Try_stack.f and client2.c. I am compiling my code as:

gcc -c client2.c
gfortran -g Try_stack.f client2.o -o combined

My Try_stack.f file:

      program circle

      call circle2
      stop
      end

      subroutine circle2 
      dimension rread(2)
      double precision r, area,rread
      external client

      area = 3.
      rread(1)=area
      rread(2)=area+10.
      write (*,*) 'Area = ',  rread(1)
      call client(rread)
      retNread = rread(1) * 2
      write(*,*) 'new nread is: ',retNread

      return
      end

And my client2.c file:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <stdint.h>

int client_(double rread[2]) 
{ 
    double result;
    result=1.;
    rread[1]=result;
    printf("%.2lf",rread);
    return 0; 
} 

After running the compiled version I am getting:

Area = 3.0000000000000000
0.00 new nread is: 6.00000000

But, I wanted the return value to the fortran program to have been equal to 8.000 instead of 6.0000 (because fortran sends the value 3., 1. is added to 3. and a 4.0 should return back to fortran for multiplying it with 2.). If I wanted to write this in a simple way to explain it, I would say:

  • First, I want the fortran file to send number 3. to c (actually I want to exchange arrays).

  • Second, I want the c file to take number 3. and add 1.

  • Third, I want c to return back the result to the fortran file, i.e. number 4.

  • Finally, I want fortran to continue computing, in this case multiply 4*2=8.

I read a lot about iso_c_binding but I have not obviously managed to utilise it, plus it requires recent versions of Fortran if my understanding is correct. Any help will be much appreciated.

KL-Yang

There are a lot of comments, did anyone actually compile and try to run this code? Beside the FORTRAN (index start form 1) and C (index start from 0), there is a typo preventing you get expected result.

BTW, please use implicit none in any FORTRAN!

int client_(double rread[2]) 
{ 
    double result;
    result=1.;
    //rread[1]=result; --> typo?
    rread[0]+=result;
    printf("%.2lf",rread);
    return 0; 
} 

Area = 3.0000000000000000
0.00 new nread is: 8.0000000000000000

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

GNU Fortran and C interoperability

From Dev

Fortran and c interoperability

From Dev

C++ and Fortran interoperability

From Dev

Calling FORTRAN subroutine from c++ yields illegal parameter value

From Dev

Calling a Fortran function from Qt C++

From Dev

Calling C from fortran (ifort, gfortran)

From Dev

Calling C function with **int parameter from Fortran

From Dev

Calling a FORTRAN subroutine from C#

From Dev

Calling C function from Fortran with pointers

From Dev

Return string from Fortran to C++

From Dev

Fortran and C interoperability: passing char *string as a function's argument to Fortran

From Dev

Pointer is being being masked when calling a C function from Fortran

From Dev

Calling Fortran subroutines with optional arguments from C++

From Dev

Calling Fortran from C# in VS2019 with iFort

From Dev

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

From Dev

Call C dll from Fortran

From Dev

C Wrapper Calling Fortran Functions

From Dev

Link to Fortran library (Lapack) from C++

From Dev

Is it possible to call a Fortran interface from C++

From Dev

Passing string from Fortran to c++

From Dev

accessing variables of fortran module from c++

From Dev

importing interface module procedure from fortran into C

From Dev

Convert a complicated condition from C to Fortran

From Dev

Passing string from Fortran dll to C#

From Dev

Difference in Fortran pointer and Fortran allocatable in calling C_F_POINTER

From Dev

Calling GSL routine CQUAD from Fortran

From

Minimal example of calling a fortran library from go

From Dev

Calling Fortran subroutines with array arguments from Julia

From Dev

Calling fortran code from Qt in QtCreator

Related Related

HotTag

Archive