Davide
Davide

Reputation: 17818

Create mod file from c header

I installed gcc and gfortran on AIX, and they work fine. Among other things I use them with some libraries which are accessed by header files (.h in C). The very same libraries can be used with modules (.mod in fortran), but as you know their use is optional: if I comment out the

use mylib
implicit none

from my fortran source code, everything works fine. Problem is, I don't want to comment out the implicit none, so I have to keep the use mylib too, otherwise I'll get a bunch of

Error: Symbol 'foo' at (1) has no IMPLICIT type

errors. And, as you know, the .mod files are compiler dependent, so I cannot use the ones I have, otherwise gfortran will spit:

Fatal Error: File 'mylib.mod' opened at (1) is not a GFORTRAN module file

Theoretically I could recompile all the libraries with gfortran, but most likely it will fail for various reasons (and if it doesn't it will still be a daunting task).

So I am wondering if there is a way to precompile the .h header in a .mod module, providing just the interface information which is all that is needed. Unfortunately, I wasn't able to find any information about this.

Upvotes: 0

Views: 2037

Answers (2)

steabert
steabert

Reputation: 6878

You could declare the functions/subroutines you use from the module as external in a custom-made file mylib.fh, for example:

#mylib.fh
external my_function

In you fortran code, you would then use:

implicit none
include 'mylib.fh'

Upvotes: 1

M. S. B.
M. S. B.

Reputation: 29391

C header files will not produce Fortran module files. Fortran module files are compiler-dependent but are normally produced when you compile Fortran files containing modules. They record information about a source-code module, such as the procedures that it contains and their calling conventions, for use by the compiler when it encounters calls to those procedures. This is the method by which the compiler makes calls to module routines automatically explicit. Typically you have to compile the Fortran-source code file containing the module (here module "mylib") before you compile a file that has a main program or procedure that uses it. Why do you assume that compiling the libraries with gfortran will most likely fail, or will be daunting? I suggest giving it a try. You can't compile a C .h file into a module. If the author of a Fortran library wanted to they could provide a file with Fortran interfaces which described the procedures and that could be compiled into a module. But that technique is error prone, because it means that there are two items that have to be kept in agreement.

If the libraries are in C and are being called from Fortran through the ISO C Binding, then the best technique is to write a file with interfaces. Was such a Fortran file provided? Were did the incorrect module files come from? When you compile that file with gfortran you will get the .mod file. While the duplication between the actual C source code and the Fortran interface declarations means two items have to be maintained, in this case it is necessary since the original source code is in C and the Fortran interface statements describe the C routines to the Fortran compiler in Fortran.

Upvotes: 2

Related Questions