Debugging CMake Fortran builds

less than 1 minute read

CMake works excellently for building very complex Fortran projects, particularly across various operating systems and HPCs. Here are a few common issues that trip up CMake builds.

Linker options

specify these for all targets with

link_libraries(-myflag)

or for a particular target with

target_link_libraries(myexe PRIVATE -myflag)

Order of libraries

The order of libraries matter–the libraries on the left “call” the libraries more toward 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
make

after adding this line.

Leave a Comment