CMake 2.6 with Fortran

CMake continues to grow in popularity including among the Fortran world as Fortran becomes more completely supported in CMake 2.6. This dovetails nicely with Gfortran 4.2 strengthening initial Fortran 2003 support. As usual, set environment variable FC=gfortran to signal the intention to use GCC to compile Fortran. A typical workflow for a Fortran program with CMake 2.6 looks like:

mkdir bin
cd bin
cmake ..
make -j

The optional GNU Make make -j parameter allows compiling in parallel if you have more than one CPU core such as an Intel Core 2 Duo or Quad. To avoid slowing everything else on the computer too much, manually specify the number of parallel build jobs like make -j3 for a Core 2 Quad. Windows users of MinGW can use cmake -G "MinGW Makefiles" to use that GCC implementation.

As we break away from the long Fortran 77 brain drain with free and low cost Fortran 95 compilers available for more platforms, it’s important to also not get stuck in 1980’s build systems. CMake abstracts away platform specific details so we don’t have to distribute a plethora of Makefiles.


Some may object that CMake adds significant configuration time, which it does–once if the CMakeLists.txt is setup correctly. Once configured, simply type

make -j

to build after code changes. Only rather major changes would require a full reconfigure. Also, CMake is immensely faster than Autotools and more platform agnostic.