Utility to generate Fortran interface for C prototypes?

Is there any (easy) working utility to generate Fortran interfaces, using iso_c_binding module, for a set of C prototypes, say placed in a C header file?

I have googled and found

  1. bindGen.rb - Ruby script in CDI package, does not seem to process my header
  2. h2m - I am unable to build it (it seems to require clang and llvm)
  3. cfwrapper.py by @vmagnin in gtk-fortran - this seems to be for gtk libraries only?

Yes, it was specifically developed to generate Fortran interfaces for GLib/GTK. You could of course hack it for other C libraries, but would have to finely tweak the code, especially the regular expressions. It depends on the complexity of the header files; the wrapper makes a first pass to clean the C preprocessor stuff before looking for C prototypes.

For C++, there is also GitHub - swig-fortran/swig: This fork of SWIG creates Fortran wrapper code from C++ headers.

I know also GitHub - eliben/pycparser: 🐍 Complete C99 parser in pure Python
Maybe it could be used to generate Fortran interfaces?

The difficulty of parsing C is that its syntax is quite versatile, and moreover it is a 2in1 language: the C language plus the C preprocessor.

Working recently on the next gtk-fortran release (GTK 4.10 / GLib 2.76), I have stumbled on a few functions like:

gboolean (g_str_has_suffix) (const gchar *str, const gchar *suffix);

I had already encountered such a syntax at the beginning of the project without understanding what it meant. That’s again a preprocessor trick: if you define a macro with that same name g_str_has_suffix, the preprocessor won’t make the substitution here because of the parentheses.
Of course, my wrapper “thought” that the name of the function was gboolean. I have refactored that part of the code and tune the regexes. Now, the analysis starts from the end of the line: the first parentheses pair (from right) is then the arguments of the function, then you have the name (possibly in parentheses), then the returned type.

My prototypes are very simple. No preprocessor directives, no abstract types, * character in pointer declarators always joining the identifier.

CBFortran (“fork” of CodeBlocks) or just CodeBlocks with “FortranProject” extension installed has some feature like described in tutorial: BindTo User Guide | CBFortran

Maybe it could be usefull in your case.

1 Like

It seems to work only one way “Fortran to C”, i.e. generating interfaces to call Fortran procedures from C

If you tell me the name of the library or if you send me the header files, I could try to use my cfwrapper on it. No guarantee, but if the prototypes are clear, we could expect a good result (partial or full).

SWIG linked upthread should work for your case.

FWIW, ChatGPT (specifically, GPT-4) does a surprisingly reasonable job generating Fortran interfaces and test code to call C functions (at least a few that I tried). I guess this question is asked often enough to provide lots of training material for it.

1 Like