CMake is perfectly capable of handling Fortran code; however, examples
are less abundant than for C or C++. In the example below we write a
very small CMakeLists.txt to wrap a simple Fortran project.
The motivation for this was to easily compile Fortran code that came
with the NORMA package. The
distribution NORMA-1.0.tgz was downloaded and unpacked. following
CMakeLists.txt was placed into
the ./software directory where the NORMA sources are stored.
To build one would typically create a build directory at the same level
mkdir build && cd buildccmake ../softwaremakemake install
and then configure, build and install.
The project is very simple:
There are only a few fortran source code files in the software
There are no library dependencies.
Executables are typically installed in a bin directory in the
package itself (i.e. NORMA/bin) although the user is free to to
change the installation prefix.
Make sure that we build a RELEASE by default (and let the user know
in the GUI); uses example code from the docs.
Set the default prefix to the non-standard installation scheme in
which one installs all component into the package directory. This
directory is assumed to be one above relative the build directory.
The full canonical path is obtained with the
As it is typical for scientific Fortran code, the compiler
optimizations are fairly specific. Here we are trying to detect a
few common ones (gfortran, Intel ifort, g77) and set the compile
options accordingly. If all else fails we use -O2 and hope for the
best (but let the user know).
The NORMA.exe binary is built from the source NORMA.f so we have
to say this explicitly with the
add_executable ("NORMA.exe" "NORMA.f")
line but the other binaries are simply built from corresponding .f
files and we write this as a simple foreach-loop.
Executables are installed in the bin dir (which is relative to the
Finally, scripts are copied to the bin directory, too.
This page was initially populated by conversion from its original location in another wiki.