Calling Routines in Fortran Modules From C

I have a module in fortran and I want to call that module from C. I use gfortran (as Fortran compiler) and gcc (as C compiler). I want to calling routines in fortran modules from C. We can solve this problem by compile our code to object file and use gcc to compile object as binary (main program coded in C). This is my fortran code (save this code as tesmodule.f).

Compile this code with command :

gfortran -O0 -g -frecord-marker=8 -c tesmodule.f

Use nm command to check module or variable from tesmodule.o object file with command :

From this output, fortran translate module tes1 to function __tesmodule__tes1. So if we want to call this module in C, we must use __tesmodule__tes1. Create C code, save as main.c. Copy this code to this file :

Create object file from main.c with command :

gcc -O0 -g -c main.c

Compile object file with command :

gcc -O0 -g -frecord-marker=8 -o tescfortran main.o tesmodule.o -lgfortran

Output from this program is :

This is an example how to calling routines in fortran modules from C. So, from this example, we can merge code from C and Fortran together.

One Comment

Add a Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.