1

I downloaded some Fortran source code which contains around 50 or so .f files. Each .f file contains one subroutine or function. I'd like to conveniently stuff all of these into a Fortran module .mod file. Is there any way of doing this with gfortran (besides painstakingly writing out each sub/func prototype inside a module definition)?

3 Answers3

1

This is more of a formatted comment than an answer, but you could try writing a source file something like this

module this_will_end_in_tears

contains

include 'subroutine1.f'
include 'subroutine2.f'
include 'subroutine2.f'
...

end module this_will_end_in_tears

If, as I suspect, the .f files contain fixed-form source, then you had better make sure the module is also in fixed-form.

I expect, as you may have guessed, that this will not compile the first time you try it, but it might (just might) save you a little time over doing the job right by, as you put it, painstakingly writing out each sub/func ...

Oh, and lest any of the Fortran-lovers out there cringe at this suggestion, note that I'm not saying it's a good way to proceed, just that it might save you a little time.

Good luck.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
  • What would the difference be between `contains`...`include` like you've shown, and copy/pasting the subroutines into the module after the `contains` stmt? Aside from all of the source code being in the same file. – Matt P Sep 26 '17 at 16:52
  • Unfortunately in my case the `.f` files are fixed form. Perhaps there is a script or tool to update source code from older fortran to newer? –  Sep 26 '17 at 17:52
  • you can make the module fixed form. (that does not preclude using free form for code that `use`s the module so long as you keep the source in seperate files. – agentp Sep 26 '17 at 18:45
  • @MattP a drawback to the `include` approach is that build software (make) may not automatically see changes to the source as requiring a module rebuild. A minor issue but something to be aware of, especially if you plan on working on the source. – agentp Sep 26 '17 at 18:52
0

I made this bash script that takes two parameters 'the module name' and 'the file extension' then builds a module file. It's presently made for fixed-format. All the source files must be present in the same directory as the script. Of course you can modify it to be more flexible.

name=$1
ext=$2
modsrc=$name.$ext
echo -e "      MODULE $name\n" > $modsrc
echo -e "          implicit none\n" >> $modsrc
echo -e "          contains\n" >> $modsrc
for i in *.f;
do
   if [ "$i" != "$modsrc" ]; then
       echo -e "         include '$i'\n" >> $modsrc
   fi
done
echo -e "       END MODULE $name\n" >> $modsrc
-1

I usually tackle things by compiling even .f90 code as -fixed. Then usually add in INTENT and IMPLICIT NONE. Almost always I have D-Lines for debug which seems to work easiest for me with -fixed form. Later adding in alignment pragmas and OpenMP reduction clauses and related vector stuff.

Holmz
  • 714
  • 7
  • 14