Personal tools
You are here: Home Articles Setting Up GNU gfortran on Windows XP
Document Actions

Setting Up GNU gfortran on Windows XP

How to install and configure the GNU Fortran 90 compiler on a Windows system

Introduction

Finally a truly open-source Fortran 90 compiler is available!  The choices for Fortran compilers in the marketplace have dwindled over the years, with the most noticeable exits from the market being Compaq (Digital) and Salford Software.  For Windows, the costly commercial choices include Absoft, Lahey, Intel, and NAG.  A Fortran 90 subset, the F language, is available for free, but the compiler is closed source.  The Free Software Foundations GNU Compiler Collection has finally implemented a functional Fortran 90 compiler.

About gfortran

The gfortran compiler actually started life known as g95, meant to replace the older and problematic g77 compiler.  Somewhere along the development path, the project maintainer of g95 began violating the terms of the GNU compiler, namely only offering binary versions of the software.  Because of the violations, some team members branched gfortran from an older g95 version and completed development of the compiler.  A more thorough history can be found here.  Long story short, the gfortran compiler is the GNU-supported Fortran 90 compiler.
At this time, gfortran can be considered a "version 1" product, meaning some bugs may be present, but it is ready for the production environment.  In the past the old GNU g77 compiler was often avoided for its lack of any language extensions, especially Cray pointers.  The new gfortran now supports Cray pointers (as of version 4.2.0), a long-demanded feature.  The GNU team also provides a compatability list to highlight the completeness of gfortran.  In my experience, gfortran is quite capable of compiling complex programs involving heavy use of Fortran 90 pointers and user-defined types.  The best way to find out if it will work for you is to try it; afterall, it is free!

Step 1: Installing the Proper Environment

In order to facilitate the use of gfortran, Approximatrix suggests installing the Cygwin environment on you Windows system.  Cygwin provides a Linux-like environment within Windows, including a complete X server and Linux shells such as bash.  Cygwin itself includes the GNU compiler collection, although the default versions are somewhat dated.  Proceed to the Cygwin homepage and download the installer.  The installer contains easy-to-follow directions.  Proceed through the screens until the Select Packages screen appears.  We now need to select some packages in preparation for gfortran. 
Open the "Devel" category.  To compile gfortran, you'll need to select the following packages:

  • autoconf (version 2.5 should suffice)
  • automake (the latest version available)
  • binutils
  • bison
  • gcc
  • gcc-core
  • make

Cygwin probably has already selected the majority of the list above by default.  Any other default selections should probably be left alone also.  Next proceed to the "Libs" category.  Select the following:

  • gmp

The library above allows gfortran to use arbitrary precision for math calculations.
At this point, feel free to install any other interesting packages that Cygwin has to offer.  When done, click "Next" and the installer will begin downloading and installing the packages.  Depending on you connection speed this could take 10 minutes or hours.

Step 2: Download Current GCC

The next step is to proceed to the GNU Compiler Collection homepage and download the current version of GCC.  This website provides source code distributions of GCC only, and that is exactly what we are looking for.  At the time of writing this article, the current version of GCC was 4.1.0, but 4.2.0 was the current development branch containing a few notable improvement to gfortran.  At this point you have a choice to make based on your preferences.  The current release will be the most stable version of gfortran available, but often the development branch will be stable enough and may contain significant improvements.  Choose whichever you feel more comfortable with.
GCC can be downloaded from a number of mirror sites.  GCC provides a list of download locations here.  Once connected to a download mirror, proceed to the directory containing the GCC version you wish to install.  There should be 8 to 9 files in the version directory.  For now download the file labeled gcc-4.x.x.tar.bz2 where the x's are replaced with the appropriate version number.  This file will be the largest as it contains all the GCC frontends, including C, C++, Fortran, Ada, and Java. 

Step 3: Getting Ready to Compile

After downloading the archive, move it to a convenient folder on a local hard drive.  For the time being let's assume you've put the archive in C:\Build_gcc.  It will not remain here when we are done, but we need a convenient directory to build in.  To be safe, do not place the archive in a path containing spaces or that is particularly long.  For example, the directory C:\Documents and Settings\Fortran Man\My Documents\work\gcc\build would be a particularly bad choice.  Open a Cygwin command window and navigate to the directory; in our example, this directory would be /cygdrive/c/Build_gcc (Cygwin uses the top-level /cygdrive directory to access drives by letter).  Extract the archive using the command:

tar -xjf gcc-4.x.x.tar.bz2
or
tar -xzf gcc-4.x.x.tar.gz

Note that I've left out the 'v' flag (verbose) because the archive has a large numebr of files, and outputting the names of each one slows the extraction process down.  After the extraction is complete, you should have a gcc directory here. 
Next, create a directory called obj, where the results of compiling the contents of gcc will be stored.   This step is necessary per the GNU GCC team as GCC cannot be built inside its own source directory.  Enter the new obj directory.
Now we need to configure the build process.  People who have used Linux may be familiar with executing configure scripts.  From the obj directory, type the following command:

../gcc/configure --help

The command should give you a summary of the options available to you.  Do not get disheartened at this point; specification of most of these options is unnecessary.  To build the Fortran compiler, I suggest the following command:

../gcc/configure --enable-languages=c,fortran

The script should do some checking for basic requirements.  Notice that we have specified that only the C and Fortran compilers should be built.  You may also specify the C++, Objective-C, and Java compilers now (Ada has significantly more requirements, including an Ada compiler), but all these will increase your build time.  Furthermore, building the Java compiler on Windows can be problematic if you also compile the Java runtime library, which often does not work with Cygwin.  Most of the other options that the configure script provides will have little or no effect on the Fortran and C compilers.

Step 4: Building gcc and gfortran

The next step is to begin building the compilers.  This process can be somewhat time consuming (we are compiling compilers after all...).  Again, from the obj directory we created, execute the following command (as suggested by the GCC-provided instructions):

make CFLAGS='-O' LIBCFLAGS='-g -O2'  LIBCXXFLAGS='-g -O2 -fno-implicit-templates' bootstrap

Now would be a good point to find something else to do for a while, like read a few chapters of that book you've been meaning to start.  Building just the C and Fortran compilers takes about 2 to 3 hours on today's 3GHz desktops.  Eventually, the process will complete.

Step 5: Installing Your New Fortran Compiler

Once the build process is complete, you have one last command to finish up.  Type the following command to install the compiler from within the obj directory:

make install

After a few moments, the installation should be complete.  The default install location will be in Cygwin's /usr/local directory, with the executables lying in the bin directory there. This installation location is desirable because it does not overwrite the default Cygwin compilers, which are all located in /usr/bin.  To make sure the installation went alright, try the following command:

gfortran --version
The compiler should respond with something similar to:
GNU Fortran 95 (GCC) 4.2.0 20060524
Copyright (C) 2006 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING

Of course, your version number and date might be different.  At this point, the compiler is ready to use.

Step 6: Makedepf90 - A Necessary Helper Application

Most of the commercial Fortran packages come with a dependency checker, necessary for determining the order in which Fortran 90 modules should be compiled.  The gfortran compiler, however, includes only the compiler.  The best solution is to download the Makedepf90 package, available here.  This package has the ability to scan Fortran 90 files to determine the order of compilation.  The package comes with good documentation and examples to help.  Download the source code distribution and extract it in a convenient directory.  Enter Makedepf90 directory and run the following commands:

./configure
make
make install

The Makedepf90 package is now ready for use.  Consult the accompanying documentation (try typing 'man makedepf90') for simple ways to use the program in a Makefile.

Conclusion

The process may have taken a while, but the process is worth it.  The gfortran compiler is a quickly maturing product. Try the newly-built compiler with some of your own programs.  If you experience problems with the compiler, the GCC team provides great bug reporting and tracking here.  Problems seem to be addressed in a timely manner, and the development team is always helpful.  Enjoy!

Addendum - The Easy Way Out...

If this whole process seemed unnecessarily difficult, the gfortran team also provides a selection of pre-built binaries here.  Two versions are offered, MinGW and Cygwin.  MinGW (Minimalist GNU for Windows) is a native windows compiler that does not require the installation or existence of the cygwin1.dll file.  This version normally comes with a stripped-down GNU toolset (make, gcc, etc.) that will work from any Windows command line.  The Cygwin version requires that Cygwin already be installed on the system. 
The major downside to using the prebuilt binaries is the lack of choice; only a single version is available to install.  If you build the compiler yourself, which is not particularly difficult, you can use any version you wish, from the current development branch to the previous stable release.  It's all a matter of choice. 


Jeffrey Armstrong
jeffrey.armstrong@approximatrix.com
September 15, 2006


Advertisements
« July 2010 »
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: