Debugging CMake Fortran builds

CMake ≥ 3.12 is excellent for building very complex Fortran projects including submodule. CMake makes building Fortran projects across various operating systems and HPCs quite robust. Here are a few common Fortran CMake patterns.

Linker options

specify for all targets with


or for a particular target with

target_link_libraries(myexe PRIVATE -myflag)

Order of libraries

The order of libraries matter. Libraries on the left “call” the libraries to the right.

undefined reference to …

occurs at linking of main executable: be sure the library is actually linked to the user library with

make VERBOSE=1

undefined reference to MOD

be sure the *.mod file directory is included, particularly to the main executable.

target_include_directories(myexe PRIVATE ${MYLIB_INCLUDE_DIRS})

Because this requires rebuilding the exe, do

make clean

after adding this line.