ISO_C_BINDING: Is it okay to create several include files?

Is it okay to break all the C routine iso_binding_c interfaces into individual Fortran include files and include them when needed (some will be used more than once), should I instead put them all in one include file, or should I just embed each individual iso_binding_c interface directly in with the Fortran routine using the interface?

Which of these options are frowned apon? Which option is more efficient? Which option is the most common to see, if so why?

Thank you,

Is there a reason why you donʼt want to bundle your interface bindings to modules? Include files are not necessary anymore in most cases since Fortran 90.

Include files may not be necessary any more but I find them very convenient because my own modules are in files whose names end with mod.f90 and I include the relevant ones in programs that need them. Of course that increases the compilation time but it saves having to remember which modules are used where, e.g. in a program I haven’t used for a while.

It seems like a fortran module is the obvious place to put such shared interfaces. It is compiled once, and then used where ever it is needed. In contrast, the include file contents must be recompiled every time it is referenced, although honestly, that is probably trivial for this situation. If there are changes to the C code, or to the interface, then that one change will then be included in all of the dependent files. That interfile dependence should be documented in a Makefile or with whatever build system you are using for your codes.

Ah. I have never got into makefiles or other complicated build systems - my use of include with modules means that I only need gfortran or ifort to compile. It’s bad enough having to keep up with Fortran and Maple to do the sorts of calculations I’m interested in.

Prior to f90, each subroutine in a fortran program could be compiled independently and then the result linked together to form the final executable program. The INCLUDE statement and MODULEs were introduced in f90 (and INCLUDE was a common extension before that). This introduced a new kind of interfile dependency to the build process for a fortran program. This dependency means that some files need to be compiled before others, and when one file is modified, then other unmodified files need to be recompiled. That compilation order requirement did not exist with f77 (and earlier), but it came into existence with f90.

However, f90 placed the entire burden of that dependency specification on the programmer, there were no facilities within the language, or standard utilities defined by the standard to assist the programmer in that task.

You are using include files in your project. When you change an include file, then you need to recompile every subroutine that references that include file. If you don’t, then you will have different definitions (different variables, in different orders, and possibly different total lengths) of the common block in different subroutines. That would be an error in your code. You cannot simply ignore this order, or ignore these dependencies.

One way to specify those dependencies is with a Makefile. The make utility is part of POSIX, so its behavior and its specification is defined by a separate standard. There are other ways, including, for example, custom shell scripts that build your program correctly, or other configuration files used by other build systems. But regardless, you must compile the pieces of your code in the correct order in order to produce a correct result. There were no compilation order requirements in f77, but you cannot ignore the order with f90 and later (with INCLUDE and/or MODULE). Of course, other information, such as compiler options and load libraries, are also specified in the Makefile, so it is important for those reasons too.

If you distribute your codes, either individually or in some open source way, then you should include this dependency information along with the fortran source. The dependency information is an essential part of the program. With some effort, that information can be regenerated from the fortran source, but there should never be any need to do that, the dependencies should be distributed along with the source code in the first place.

Even if your program is all within one file, if you use modules, then the order of the modules and the subprograms that use them must be in the correct order within that file. Otherwise, you can have inconsistent builds even within a single-file program. That is, you can have a subprogram that uses an out-of-date *.mod file that was created during a prior compilation attempt.

2 Likes