diff --git a/m4/ax_mexopts.m4 b/m4/ax_mexopts.m4
index 8c1150287d23ed0a80202541246b03e816da4e5a..52c7505811a56d08ee5660f54ddb6259cd29a325 100644
--- a/m4/ax_mexopts.m4
+++ b/m4/ax_mexopts.m4
@@ -60,8 +60,10 @@ case ${MATLAB_ARCH} in
     # Also note that the -lssp must come by the end of the link command
     # (otherwise it will have to be enclosed within --whole-archive).
     MATLAB_LIBS="-lmex -lmx -lmat -lmwlapack -lmwblas -Wl,-Bstatic -lssp -Wl,-Bdynamic"
-    # Hack for static linking of libgomp, needed for OpenMP
-    OPENMP_LDFLAGS="-Wl,-Bstatic,--whole-archive -lgomp -Wl,-Bdynamic,--no-whole-archive"
+    # Hack for static linking of libgomp, needed for OpenMP.
+    # Should come by the end of the link command
+    # (otherwise it will have to be enclosed within --whole-archive).
+    OPENMP_LIBS="-Wl,-Bstatic -lgomp -Wl,-Bdynamic"
     ax_mexopts_ok="yes"
     ;;
   maci64)
@@ -129,5 +131,5 @@ AC_SUBST([MATLAB_CXXFLAGS])
 AC_SUBST([MATLAB_FCFLAGS])
 AC_SUBST([MATLAB_LDFLAGS])
 AC_SUBST([MATLAB_LIBS])
-AC_SUBST([OPENMP_LDFLAGS])
+AC_SUBST([OPENMP_LIBS])
 ])
diff --git a/mex/build/kronecker.am b/mex/build/kronecker.am
index ac07bb602029bd2331d225c5c58a68e7f71f6b7d..9308edf08d6b9318716e5d3c389020f7b7895e76 100644
--- a/mex/build/kronecker.am
+++ b/mex/build/kronecker.am
@@ -4,7 +4,7 @@ nodist_sparse_hessian_times_B_kronecker_C_SOURCES = sparse_hessian_times_B_krone
 nodist_A_times_B_kronecker_C_SOURCES = A_times_B_kronecker_C.f08 matlab_mex.F08 blas_lapack.F08
 
 sparse_hessian_times_B_kronecker_C_CXXFLAGS = $(AM_CXXFLAGS) -fopenmp
-sparse_hessian_times_B_kronecker_C_LDFLAGS = $(AM_LDFLAGS) $(OPENMP_LDFLAGS)
+sparse_hessian_times_B_kronecker_C_LDADD = $(OPENMP_LIBS)
 
 BUILT_SOURCES = $(nodist_sparse_hessian_times_B_kronecker_C_SOURCES) $(nodist_A_times_B_kronecker_C_SOURCES)
 CLEANFILES = $(nodist_sparse_hessian_times_B_kronecker_C_SOURCES) $(nodist_A_times_B_kronecker_C_SOURCES)
diff --git a/mex/build/local_state_space_iterations.am b/mex/build/local_state_space_iterations.am
index e9281345f32772c79e6565f1223d300a86ccd521..ddff330da4f787a62ad607a349d3f80e15b055e6 100644
--- a/mex/build/local_state_space_iterations.am
+++ b/mex/build/local_state_space_iterations.am
@@ -5,7 +5,7 @@ nodist_local_state_space_iteration_3_SOURCES = local_state_space_iteration_3.f08
 nodist_local_state_space_iteration_k_SOURCES = local_state_space_iteration_k.f08
 
 local_state_space_iteration_2_CXXFLAGS = $(AM_CXXFLAGS) -fopenmp
-local_state_space_iteration_2_LDFLAGS = $(AM_LDFLAGS) $(OPENMP_LDFLAGS)
+local_state_space_iteration_2_LDADD = $(OPENMP_LIBS)
 
 local_state_space_iteration_3_FCFLAGS = $(AM_FCFLAGS) -I../libkordersim -pthread
 local_state_space_iteration_3_LDADD = ../libkordersim/libkordersim.a 
diff --git a/mex/build/perfect_foresight_problem.am b/mex/build/perfect_foresight_problem.am
index 2c186d815ca188f4794e76f902e61341d59b58d7..6046a09c22ba83166fae8e5cfcbf0da4e322a180 100644
--- a/mex/build/perfect_foresight_problem.am
+++ b/mex/build/perfect_foresight_problem.am
@@ -6,8 +6,7 @@ nodist_perfect_foresight_problem_SOURCES = perfect_foresight_problem.cc DynamicM
 
 perfect_foresight_problem_CPPFLAGS = $(AM_CPPFLAGS) -I$(TOPDIR)
 perfect_foresight_problem_CXXFLAGS = $(AM_CXXFLAGS) -fopenmp
-perfect_foresight_problem_LDFLAGS = $(AM_LDFLAGS) $(OPENMP_LDFLAGS)
-perfect_foresight_problem_LDADD = $(LIBADD_DLOPEN)
+perfect_foresight_problem_LDADD = $(LIBADD_DLOPEN) $(OPENMP_LIBS)
 
 BUILT_SOURCES = $(nodist_perfect_foresight_problem_SOURCES)
 CLEANFILES = $(nodist_perfect_foresight_problem_SOURCES)
diff --git a/mex/build/qmc_sequence.am b/mex/build/qmc_sequence.am
index 5ee862daeaa3b55ecc8f719e6a577ac33f00a103..2cb09b2bfe9379a78650bf2d6f5a242fd1ad1814 100644
--- a/mex/build/qmc_sequence.am
+++ b/mex/build/qmc_sequence.am
@@ -4,7 +4,7 @@ TOPDIR = $(top_srcdir)/../../sources/sobol
 
 qmc_sequence_CPPFLAGS = $(AM_CPPFLAGS) -I$(TOPDIR)
 qmc_sequence_CXXFLAGS = $(AM_CXXFLAGS) -fopenmp
-qmc_sequence_LDFLAGS = $(AM_LDFLAGS) $(OPENMP_LDFLAGS)
+qmc_sequence_LDADD = $(OPENMP_LIBS)
 
 nodist_qmc_sequence_SOURCES = qmc_sequence.cc