diff --git a/.gitignore b/.gitignore
index 6b6da23d4e45996cdf99afbf88a811e63c70d17d..922ab65f377a9997c900939ab66c09c8ea9c37bb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -90,8 +90,16 @@ doc/internals/ltxpng
 # Symbolic links created for building MEX files
 /mex/build/matlab/*/*.c
 /mex/build/matlab/*/*.cc
+/mex/build/matlab/*/*.f08
+/mex/build/matlab/*/*.F08
 /mex/build/octave/*/*.c
 /mex/build/octave/*/*.cc
+/mex/build/octave/*/*.f08
+/mex/build/octave/*/*.F08
+
+# Fortran modules
+/mex/build/matlab/*/*.mod
+/mex/build/octave/*/*.mod
 
 # Dynare++
 /dynare++/integ/src/quadrature-points.dSYM/
diff --git a/m4/ax_mexopts.m4 b/m4/ax_mexopts.m4
index 3fe29551e7d01da09f60aac33391a5e07759b49e..890397b61ce46f051da6d29680bc72f76cd813fd 100644
--- a/m4/ax_mexopts.m4
+++ b/m4/ax_mexopts.m4
@@ -33,7 +33,7 @@ case ${MATLAB_ARCH} in
     MATLAB_DEFS="$MATLAB_DEFS -D_GNU_SOURCE -DNDEBUG"
     MATLAB_CFLAGS="-fexceptions -fPIC -pthread -g -O2"
     MATLAB_CXXFLAGS="-fPIC -pthread -g -O2"
-    MATLAB_FFLAGS="-fPIC -g -O2 -fexceptions"
+    MATLAB_FCFLAGS="-fPIC -g -O2 -fexceptions"
     MATLAB_LDFLAGS_NOMAP="-shared -Wl,--no-undefined -Wl,-rpath-link,$MATLAB/bin/${MATLAB_ARCH} -L$MATLAB/bin/${MATLAB_ARCH}"
     MATLAB_LDFLAGS="$MATLAB_LDFLAGS_NOMAP -Wl,--version-script,$MATLAB/extern/lib/${MATLAB_ARCH}/mexFunction.map"
     MATLAB_LIBS="-lmx -lmex -lmat -lm -lstdc++ -lmwlapack -lmwblas"
@@ -50,13 +50,14 @@ case ${MATLAB_ARCH} in
   win32 | win64)
     MATLAB_CFLAGS="-fexceptions -g -O2"
     MATLAB_CXXFLAGS="-g -O2"
-    MATLAB_FFLAGS="-fexceptions -g -O2 -fno-underscoring"
+    MATLAB_FCFLAGS="-g -O2 -fexceptions -fno-underscoring"
     MATLAB_DEFS="$MATLAB_DEFS -DNDEBUG"
-    # Note that static-libstdc++ is only supported since GCC 4.5 (but generates no error on older versions)
+    # The hack for libquadmath is needed because -static-libgfortran
+    # unfortunately does not imply the static linking of the former.
     # The last part about winpthread is a hack to avoid dynamically linking
     # against libwinpthread DLL (which is pulled in by libstdc++, even without
     # using threads, since we are using the POSIX threads version of MinGW)
-    MATLAB_LDFLAGS_NOMAP="-static-libgcc -static-libstdc++ -shared -L$MATLAB/bin/${MATLAB_ARCH} -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,-Bdynamic,--no-whole-archive"
+    MATLAB_LDFLAGS_NOMAP="-static-libgcc -static-libstdc++ -static-libgfortran -Wl,-Bstatic,--whole-archive -lquadmath -Wl,-Bdynamic,--no-whole-archive -shared -L$MATLAB/bin/${MATLAB_ARCH} -Wl,-Bstatic,--whole-archive -lwinpthread -Wl,-Bdynamic,--no-whole-archive"
     MATLAB_LDFLAGS="$MATLAB_LDFLAGS_NOMAP \$(abs_top_srcdir)/mex.def"
     MATLAB_LIBS="-lmex -lmx -lmat -lmwlapack -lmwblas"
     # Hack for static linking of libgomp, needed for OpenMP
@@ -68,7 +69,7 @@ case ${MATLAB_ARCH} in
     MATLAB_DEFS="$MATLAB_DEFS -DNDEBUG"
     MATLAB_CFLAGS="-fno-common -arch x86_64 -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET -fexceptions"
     MATLAB_CXXFLAGS="$MATLAB_CFLAGS"
-    MATLAB_FFLAGS="-fexceptions -fbackslash -arch x86_64"
+    MATLAB_FCFLAGS="-g -O2 -fexceptions -fbackslash -arch x86_64"
     MATLAB_LDFLAGS_NOMAP="-Wl,-twolevel_namespace -undefined error -arch x86_64 -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET -bundle"
     MATLAB_LDFLAGS="$MATLAB_LDFLAGS_NOMAP -Wl,-exported_symbols_list,\$(abs_top_srcdir)/mexFunction-MacOSX.map"
     MATLAB_LIBS="-L$MATLAB/bin/maci64 -lmx -lmex -lmat -lmwlapack -lmwblas"
@@ -79,11 +80,6 @@ case ${MATLAB_ARCH} in
     ;;
 esac
 
-# Starting from MATLAB 7.8, on 64-bit platforms, BLAS and LAPACK expect 64-bit integers, so make it the default for integers in Fortran code
-if test "${MATLAB_ARCH}" = "glnxa64" -o "${MATLAB_ARCH}" = "win64" -o "${MATLAB_ARCH}" = "maci64"; then
-  AX_COMPARE_VERSION([$MATLAB_VERSION], [ge], [7.8], [MATLAB_FFLAGS="$MATLAB_FFLAGS -fdefault-integer-8"])
-fi
-
 # Kludge for incompatibility of older MATLABs (≤ R2011a) with recent gcc
 # Include <uchar.h>, because matrix.h needs char16_t
 AX_COMPARE_VERSION([$MATLAB_VERSION], [le], [7.12], [MATLAB_CFLAGS="$MATLAB_CFLAGS -include uchar.h"])
@@ -132,6 +128,7 @@ AC_SUBST([MATLAB_CPPFLAGS])
 AC_SUBST([MATLAB_DEFS])
 AC_SUBST([MATLAB_CFLAGS])
 AC_SUBST([MATLAB_CXXFLAGS])
+AC_SUBST([MATLAB_FCFLAGS])
 AC_SUBST([MATLAB_LDFLAGS])
 AC_SUBST([MATLAB_LIBS])
 AC_SUBST([OPENMP_LDFLAGS])
diff --git a/mex/build/matlab/configure.ac b/mex/build/matlab/configure.ac
index 88420331fff97de939c97545d891cd12c80c84e6..11a101f1668f2712291b8d774b72710ab1b93e7c 100644
--- a/mex/build/matlab/configure.ac
+++ b/mex/build/matlab/configure.ac
@@ -46,10 +46,10 @@ case ${host_os} in
 esac
 
 CFLAGS="$MATLAB_CFLAGS -Wall -Wno-parentheses"
-FFLAGS="$MATLAB_FFLAGS -Wall"
+FCLAGS="$MATLAB_FCFLAGS -Wall"
 CXXFLAGS="$MATLAB_CXXFLAGS -Wall -Wno-parentheses -Wold-style-cast"
 
-AC_PROG_F77([gfortran g77 f77])
+AC_PROG_FC
 AC_PROG_CC
 AC_PROG_CC_C99 # mjdgges DLL now uses C99 features (variable declared in for loop)
 AC_PROG_CXX
diff --git a/mex/build/matlab/mex.am b/mex/build/matlab/mex.am
index 7a91726c4198279b034663377bda37f5ab0cf6e1..8ea6ac2bb8eedb667cb1d752a6caa00f209721cc 100644
--- a/mex/build/matlab/mex.am
+++ b/mex/build/matlab/mex.am
@@ -24,6 +24,11 @@ all-local: $(PROGRAMS)
 	fi
 
 clean-local:
+	rm -f *.mod # Fortran modules
 	if test -d $(top_srcdir)/../../matlab; then \
 		cd $(top_srcdir)/../../matlab && rm -f $(PROGRAMS); \
 	fi
+
+# Automake provides a default rule for .f08 files, but not .F08
+%.o: %.F08
+	$(AM_V_FC)$(FCCOMPILE) $(DEFS) -c -o $@ $<
diff --git a/mex/build/octave/configure.ac b/mex/build/octave/configure.ac
index 56ebe1eb16a70417c2ef5dac78a5967109fb1532..cee2239f66daa0587ad5b09127c1ae38cd861f0e 100644
--- a/mex/build/octave/configure.ac
+++ b/mex/build/octave/configure.ac
@@ -28,6 +28,7 @@ test "$MKOCTFILE" = no && AC_MSG_ERROR([Octave cannot be found])
 CC=$($MKOCTFILE -p CC)
 CXX=$($MKOCTFILE -p CXX)
 CFLAGS="$($MKOCTFILE -p CFLAGS) -Wall -Wno-parentheses"
+FCFLAGS="-g -O2 -Wall"
 FFLAGS="$($MKOCTFILE -p FFLAGS) -Wall"
 CXXFLAGS="$($MKOCTFILE -p CXXFLAGS) -Wall -Wno-parentheses -Wold-style-cast"
 LDFLAGS="$($MKOCTFILE -p LFLAGS) $($MKOCTFILE -p LDFLAGS)"
@@ -35,7 +36,7 @@ LDFLAGS="$($MKOCTFILE -p LFLAGS) $($MKOCTFILE -p LDFLAGS)"
 OCTAVE_VERSION=$($MKOCTFILE -v 2>&1 | sed 's/mkoctfile, version //')
 AX_COMPARE_VERSION([$OCTAVE_VERSION], [lt], [4.2], [AC_MSG_ERROR([Your Octave is too old, please upgrade to version 4.2 at least (or disable Octave support with --disable-octave).])])
 
-AC_PROG_F77([gfortran g77 f77])
+AC_PROG_FC
 AC_PROG_CC
 AC_PROG_CC_C99 # mjdgges DLL now uses C99 features (variable declared in for loop)
 AC_PROG_CXX
diff --git a/mex/build/octave/mex.am b/mex/build/octave/mex.am
index f0da052beec5b86b93014806821871c652559451..005979f80bc4e7f15e86887dea2f83a3005408f5 100644
--- a/mex/build/octave/mex.am
+++ b/mex/build/octave/mex.am
@@ -31,6 +31,11 @@ all-local: $(PROGRAMS)
 	fi
 
 clean-local:
+	rm -f *.mod # Fortran modules
 	if test -d $(top_srcdir)/../../octave; then \
 		cd $(top_srcdir)/../../octave && rm -f $(PROGRAMS); \
 	fi
+
+# Automake provides a default rule for .f08 files, but not .F08
+%.o: %.F08
+	$(AM_V_FC)$(FCCOMPILE) $(DEFS) -c -o $@ $<