Tuesday, February 12, 2013




Fortran binding in C

Most of the libraries or useful routines are in FORTRAN and if you intend to use them in your C code, here are 2 ways of how to use them. Warning working with multi-dimensional arrays should include the row-major behaviour of C and column-major behaviour of FORTRAN. Also, take care of the data types compatibility across the two languages.


Part A - Using .f / .f90 directly

1. Compile the .f / .f90 files to .o object file using;

FC -c -O filename.f 
This generates the filename.o object file and can be used to as a C function routine with little additional effort.

2. Define the function before your main() call and pass by reference the pointers and data as suitable. For more explanation see;

http://www.math.utah.edu/software/c-with-fortran.html
http://www.physics.utah.edu/~detar/phys6720/handouts/fortran_binding.html

Part B - Convert your .f / .f90 to .c files using f2c converter

Perhaps a more easy way (if you know what you are doing !)

1. Convert the simple .f / .f90 routines to C using f2c [ http://netlib.org/f2c/ ]

2. Include the f2c library and include file while converting.

Always check for the consistency of the output after the compile & run parts of the code.

0 comments:

Subscribe to RSS Feed Follow me on Twitter!