Makefile – Compile SWAT using gfortran without modification.

Note: The makefile here is an older version. For the latest version, please visit another post SWAT Makefile Updated – Stop running when overflow happens.

Quick Download Link

Makefile Generator, Debug Makefile, Release Makefile

Next post will be: Use Makefile in Photran to Debug SWAT

Please note that the modparm.f doesn’t need to be compiled as parm.mod any more. That process is already included in the Makefile.


  • Why some files fail to be compiled in Photran?
  • Use additional gfortran command options to compile problematic files
  • Makefile
  • Generate Makefile
  • The only modification
  • Use Makefile

In my previous post Compile and Run SWAT with Photran + MinGW – the easiest way to remove mathematical errors from SWAT, several swat source code files need to be modified to be able to compilable within Photran. It’s nice, but it’s even better if the source codes could be compiled without any modification.

Why some files fail to be compiled in Photran?

Compare the original source codes and the modified source codes through Google codes, and find that only following files need to be modified.

  • biozone.f
  • bmpinit.f
  • carbon_zhang2.f90
  • main.f
  • modparm.f
  • ovr_sed.f
  • percmain.f
  • rthsed.f

Except for main.f, these files have a common problem: one or more lines exceed the default line length set for Fotran fix format file (*.f) and free format file (*.f90). The default limitation of the line length for *.f is 72 and for *.f90 is 132. The default Fortran compile command (shown below) in Photran doesn’t consider this situable. If the length of one line exceed the limitation, the compiler (gfortran) would give errors.

  • Debug version: gfortran -funderscoring -O0 -g -Wall -c -fmessage-length=0
  • Release version: gfortran -funderscoring -O3 -Wall -c -fmessage-length=0

Use additional gfortran command options to compile problematic files

To solve this problem, these files are modified to make sure they meet the requirement on line length in previous post. Besides this, there is also another solution: change the line length limitation using gfortran command options.

  • Fixed format (*.f): add -ffixed-line-length-132 option to set the line length limitation of fixed format Fortran file to 132
  • Freeformat (*.f): add -ffree-line-length-200
    option to set the line length limitation of free format Fortran file to 200

For example, the gfortran command for debug version of biozone.f would be

gfortran -funderscoring -O0 -g -Wall -c -fmessage-length=0 -ffixed-line-length-132

And the gfortran command for release version of carbon_zhang2.f90 would be

gfortran -funderscoring -O0 -g -Wall -c -fmessage-length=0 -ffree-line-length-200

These files could be compiled successfully with the new command.


In default Photran settings, it’s impossible to use different command options for different source files. Applying -ffixed-line-length-132 to other *.f files would also generate compile errors. How to solve this? Using Makefile.

Makefile defines how to compile and link a program. With it, it’s possible to define different commands for different source files and compile SWAT source codes without modification. For more information, please refer to GNU make document.

Generate Makefile

There are more than 300 Fortran source files in SWAT. It’s time-consuming work to write the Makefile manually. We already know the gfortran commands and the files which need to re-set the line length limitation. A program, which would get all *.f and *.f90 in the SWAT source codes folder and generate the compile commands in the makefile, is a quite straightforward and simple way to do the work.

Based on this, a .NET console application is created (source codes, executable). It’s fairly easy to use. Copy the program file to the source codes folder and double-click.

A prompt window would pop-up to ask which version should be the Makefile generated for, debug or release. Input d or debug for debug version, r or release for release version. Then the program would start to work and generate the corresponding Makefile in debug or release folder. The debug/release folder would be created if it doesn’t exist.

The two version of Makefile for rev 615 are also available to be download directly (debug version, release version). These two files could be also used for also version as long as the number and name of source codes files is same. Create the debug/release folder in the source codes folder and put the file into the corresponding folder.

The only modification

The only modification is to comment the first line of main.f (include modparm.f). There should be some command option to ignore this line but haven’t found it:)

Use Makefile

Makefile is fairly easy to use. Just use the GNU make command which comes along with MinGW. There are two version of make in MinGW: mingw32-make and make. Either one is ok.

Open cmd.exe and navigate to the Makefile folder. Execute mingw32-make or make. It’s done!


© 2000 – 2021 SWAT Output Viewer All rights reserved