what is the proper Fortran C interface ?

General discussion about Elmer
Post Reply
spacedout
Posts: 41
Joined: 30 Mar 2020, 23:27
Antispam: Yes

what is the proper Fortran C interface ?

Post by spacedout »

Hi

assuming my fortran and C source files are correctly written where the fortran file calls a function inside the C file, are the following two Linux commands also correct ?

gcc -shared -o myCfile.so -fPIC myCfile.C
elmerf90 -shared -o myFortfile.so -fPIC myFortfile.F90 myCfile.so

( I intend to then execute

ElmerSolver mycase.sif

where

...
Solver
Procedure = myFortfile ....

will be inserted in the mycase.sif file)
spacedout
Posts: 41
Joined: 30 Mar 2020, 23:27
Antispam: Yes

Re: what is the proper Fortran C interface ?

Post by spacedout »

the following might be a clue. This is how you create a shared file (.so) from object files (.o)

elmerf90 f1.o f2.o -o f1.so

when a fortran file f.F90 is compiled with:

elmerf90 -c f.F90 (yielding f.o)

ElmerSolver.exe dows not complain about the above recipe.

unfortunately if instead we compile a C file f2.C using

gcc -c -fPIC f2.c -o f2.o

and let f1.F90 call a C function myfcn(a,b,c,d) defined in f2.C

then,

elmerf90 f1.o f2.o -o f1.so

still results in a file f1.so

but ElmerSolver.exe will spew out weird messages like
it cannot find the file f1.so
and finally outputs undefined function myfcn

And yet f1.F90 contains

....
interface

function myfcn(a,b,c,d) bind(c)

use DefUtils
use iso_c_binding
REAL(KIND=dp), VALUE :: a, b, c, d
end function myfcn

....

and f2.C contains

extern "C"
{

#include <math.h>
#include <float.h>

double myfcn(double a, double b, double c, double d)
a,b,c,d)
{

....

}
}
spacedout
Posts: 41
Joined: 30 Mar 2020, 23:27
Antispam: Yes

Re: what is the proper Fortran C interface ?

Post by spacedout »

It may be better to execute
g++ -c -fPIC f2.c -o f2.o

for a C++ file instead of

gcc -c -fPIC f2.c -o f2.o

since g++ can link to C++ libraries

And,according to people familiar with linux and gfortran, when building .so files, it is safer to also execute

gfortran -c -fPIC f.F90

instead of just

gfortran -c f.F90

so I decided to execute

elmerf90 -c -fPIC f.F90

since elmerf90.bat does not contain a -fPIC option

However the most important thing to do in order to avoid getting an undefined function myfcn from the ElmerSolver output is for f1.F90 to have the following interface

interface

function myfcn(a,b,c,d) bind(c, name = "myFcn") ! instead of just bind(c) when the C file contains a function myFcn
use DefUtils
use iso_c_binding
REAL(KIND=dp), VALUE :: a, b, c, d
end function myfcn

end interface

This is because Fortran is not case sensitive as opposed to C.

I close this thread since I realize it does not really belong to Elmer forums. Anybody who wants to further his education on interfacing Fortran with C
should for example study the page

https://gcc.gnu.org/onlinedocs/gfortran ... ity-with-C

and perhaps search for other pages that involve the building of shared libraries.

My deepest regrets for wasting you time viewing this thread.
annier
Posts: 1141
Joined: 27 Aug 2013, 13:51
Antispam: Yes

Re: what is the proper Fortran C interface ?

Post by annier »

Dear spacedout,
Thank you very much for posting a very useful information.
This can be very useful when trying to combine CALPHAD database with ElmerSolver through software interface.

Reference: https://www.sciencedirect.com/science/a ... via%3Dihub

Both Opencalphad and Elmer are in FORTRAN language.
One of the software interfaces of OpenCalphad is available in C++ language.
One of the reasons how the software interface was developed to couple fortran codes of opencalphad (http://www.opencalphad.com/) with the C++ codes of openphase software (http://openphase.de/) -a finite difference method based software.


Yours Sincerely,
Anil Kunwar
Anil Kunwar
Department of Materials Engineering, KU Leuven, Belgium
Post Reply