Compilers typically have an option to specify static linking of the compiler libraries. This can help mitigate “missing libgfortran” errors, particularly on Windows. This problem arises because the developer may switch compiler environments, or the end user may not have compiler libraries with a compatible ABI on PATH (Windows), LD_LIBRARY_PATH (Linux), LIBRARY_PATH (MacOS).
Build environment switching is especially common on Windows. Relatively few Windows users have compilers installed. Missing DLLs have long been a hallmark of distributing Windows programs in various programming languages. A typical error code is -1073741515 corresponding to hex error code 135.
GCC / Gfortran can statically link the compiler libraries e.g. libgfortran into the final executable. This increases the size of the executable, but the extra space may be negligible compared to the nuisances avoided.
gfortran -static -static-libgfortran myprog.f90 mylib.f90 -o myprog.exe
Implement GCC static executable linking in CMake like:
Other compilers have similar static compiler library link options:
- clang -static
- flang -static-flang-libs
- ifx -static-intel
We don’t universally apply static linking because some libraries may only be available as dynamic. MacOS static linked executable may only work on your specific computer; MacOS prefers dynamic linking. This technique does not guarantee portable executables, which are a more general problem. Using a slim libc like musl can be part of the solution for portable static executables.