diff --git a/license.txt b/license.txt
index 7a5a54ef2d3457847a0d314920772f298a1b174f..1d1eee5f52c9901a85d8c3cefe86fcb0a06ec0b1 100644
--- a/license.txt
+++ b/license.txt
@@ -305,16 +305,3 @@ License: GPL-2+ with special Autoconf exception
  configuration script generated by GNU Autoconf, you may include
  it under the same distribution terms that you use for the rest
  of that program.
-
-Files: m4/gsl.m4
-Copyright: 2000, Christopher R. Gabriel <cgabriel@linux.it>
-License: GPL-3+
- GSL is free software, you can redistribute it and/or modify it under
- the terms of the GNU General Public License.
- .
- The GNU General Public License does not permit this software to be
- redistributed in proprietary programs.
- .
- This library is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
diff --git a/m4/ax_gsl.m4 b/m4/ax_gsl.m4
new file mode 100644
index 0000000000000000000000000000000000000000..314d162d7b658d378a8d0a4f10027dbe2aec3c2a
--- /dev/null
+++ b/m4/ax_gsl.m4
@@ -0,0 +1,66 @@
+dnl Detect the GSL.
+dnl We don't use the official M4 macro since it relies on the script gsl-config,
+dnl which does not work when cross-compiling.
+dnl
+dnl Copyright (C) 2010 Dynare Team
+dnl
+dnl This file is part of Dynare.
+dnl
+dnl Dynare is free software: you can redistribute it and/or modify
+dnl it under the terms of the GNU General Public License as published by
+dnl the Free Software Foundation, either version 3 of the License, or
+dnl (at your option) any later version.
+dnl
+dnl Dynare is distributed in the hope that it will be useful,
+dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
+dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+dnl GNU General Public License for more details.
+dnl
+dnl You should have received a copy of the GNU General Public License
+dnl along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
+
+AC_DEFUN([AX_GSL],
+[
+AC_ARG_WITH(gsl, AC_HELP_STRING([--with-gsl=DIR], [prefix to GSL installation]),
+            gsl_prefix="$withval", gsl_prefix="")
+
+  has_gsl=yes
+
+  if test "x$gsl_prefix" != "x"; then
+    GSL_CPPFLAGS="-I$withval/include"
+    GSL_LDFLAGS="-L$withval/lib"
+  else
+    GSL_CPPFLAGS=""
+    GSL_LDFLAGS=""
+  fi
+
+  ac_save_CPPFLAGS="$CPPFLAGS"
+  ac_save_LDFLAGS="$LDFLAGS"
+  ac_save_LIBS="$LIBS"
+
+  LIBS=""
+  CPPFLAGS="$CPPFLAGS $GSL_CPPFLAGS"
+  LDFLAGS="$LDFLAGS $GSL_LDFLAGS"
+
+	AC_LANG_PUSH(C)
+  AC_CHECK_HEADER([gsl/gsl_cdf.h], [], [has_gsl=no])
+	AC_LANG_POP(C)
+
+  AC_CHECK_LIB([m], [cos])
+  AC_CHECK_LIB([gslcblas], [cblas_dgemm], [LIBS="-lgslcblas $LIBS"], [has_gsl=no])
+  AC_CHECK_LIB([gsl], [gsl_cdf_ugaussian_P], [LIBS="-lgsl $LIBS"], [has_gsl=no])
+
+  if test "x$has_gsl" = "xyes"; then
+    GSL_LIBS="$LIBS"
+  else
+    GSL_LIBS=""
+  fi
+
+  CPPFLAGS="$ac_save_CPPFLAGS"
+  LDFLAGS="$ac_save_LDFLAGS"
+  LIBS="$ac_save_LIBS"
+
+  AC_SUBST(GSL_CPPFLAGS)
+  AC_SUBST(GSL_LDFLAGS)
+  AC_SUBST(GSL_LIBS)
+])
diff --git a/m4/gsl.m4 b/m4/gsl.m4
deleted file mode 100644
index fc7fb44294abe842141bf3bfd1e2beb2e9736d78..0000000000000000000000000000000000000000
--- a/m4/gsl.m4
+++ /dev/null
@@ -1,162 +0,0 @@
-# Configure path for the GNU Scientific Library
-# Christopher R. Gabriel <cgabriel@linux.it>, April 2000
-
-
-AC_DEFUN([AX_PATH_GSL],
-[
-AC_ARG_WITH(gsl-prefix,[  --with-gsl-prefix=PFX   Prefix where GSL is installed (optional)],
-            gsl_prefix="$withval", gsl_prefix="")
-AC_ARG_WITH(gsl-exec-prefix,[  --with-gsl-exec-prefix=PFX Exec prefix where GSL is installed (optional)],
-            gsl_exec_prefix="$withval", gsl_exec_prefix="")
-AC_ARG_ENABLE(gsltest, [  --disable-gsltest       Do not try to compile and run a test GSL program],
-		    , enable_gsltest=yes)
-
-  if test "x${GSL_CONFIG+set}" != xset ; then
-     if test "x$gsl_prefix" != x ; then
-         GSL_CONFIG="$gsl_prefix/bin/gsl-config"
-     fi
-     if test "x$gsl_exec_prefix" != x ; then
-        GSL_CONFIG="$gsl_exec_prefix/bin/gsl-config"
-     fi
-  fi
-
-  AC_PATH_PROG(GSL_CONFIG, gsl-config, no)
-  min_gsl_version=ifelse([$1], ,0.2.5,$1)
-  AC_MSG_CHECKING(for GSL - version >= $min_gsl_version)
-  no_gsl=""
-  if test "$GSL_CONFIG" = "no" ; then
-    no_gsl=yes
-  else
-    GSL_CFLAGS=`$GSL_CONFIG --cflags`
-    GSL_LIBS=`$GSL_CONFIG --libs`
-
-    gsl_major_version=`$GSL_CONFIG --version | \
-           sed 's/^\([[0-9]]*\).*/\1/'`
-    if test "x${gsl_major_version}" = "x" ; then
-       gsl_major_version=0
-    fi
-
-    gsl_minor_version=`$GSL_CONFIG --version | \
-           sed 's/^\([[0-9]]*\)\.\{0,1\}\([[0-9]]*\).*/\2/'`
-    if test "x${gsl_minor_version}" = "x" ; then
-       gsl_minor_version=0
-    fi
-
-    gsl_micro_version=`$GSL_CONFIG --version | \
-           sed 's/^\([[0-9]]*\)\.\{0,1\}\([[0-9]]*\)\.\{0,1\}\([[0-9]]*\).*/\3/'`
-    if test "x${gsl_micro_version}" = "x" ; then
-       gsl_micro_version=0
-    fi
-
-    if test "x$enable_gsltest" = "xyes" ; then
-      ac_save_CFLAGS="$CFLAGS"
-      ac_save_LIBS="$LIBS"
-      CFLAGS="$CFLAGS $GSL_CFLAGS"
-      LIBS="$LIBS $GSL_LIBS"
-
-      rm -f conf.gsltest
-      AC_TRY_RUN([
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-char* my_strdup (const char *str);
-
-char*
-my_strdup (const char *str)
-{
-  char *new_str;
-  
-  if (str)
-    {
-      new_str = (char *)malloc ((strlen (str) + 1) * sizeof(char));
-      strcpy (new_str, str);
-    }
-  else
-    new_str = NULL;
-  
-  return new_str;
-}
-
-int main (void)
-{
-  int major = 0, minor = 0, micro = 0;
-  int n;
-  char *tmp_version;
-
-  system ("touch conf.gsltest");
-
-  /* HP/UX 9 (%@#!) writes to sscanf strings */
-  tmp_version = my_strdup("$min_gsl_version");
-
-  n = sscanf(tmp_version, "%d.%d.%d", &major, &minor, &micro) ;
-
-  if (n != 2 && n != 3) {
-     printf("%s, bad version string\n", "$min_gsl_version");
-     exit(1);
-   }
-
-   if (($gsl_major_version > major) ||
-      (($gsl_major_version == major) && ($gsl_minor_version > minor)) ||
-      (($gsl_major_version == major) && ($gsl_minor_version == minor) && ($gsl_micro_version >= micro)))
-     { 
-       exit(0);
-     }   
-   else
-     {
-       exit(1);
-     }
-}
-
-],, no_gsl=yes,[echo $ac_n "cross compiling; assumed OK... $ac_c"])
-       CFLAGS="$ac_save_CFLAGS"
-       LIBS="$ac_save_LIBS"
-     fi
-  fi
-  if test "x$no_gsl" = x ; then
-     AC_MSG_RESULT(yes)
-     ifelse([$2], , :, [$2])     
-  else
-     AC_MSG_RESULT(no)
-     if test "$GSL_CONFIG" = "no" ; then
-       echo "*** The gsl-config script installed by GSL could not be found"
-       echo "*** If GSL was installed in PREFIX, make sure PREFIX/bin is in"
-       echo "*** your path, or set the GSL_CONFIG environment variable to the"
-       echo "*** full path to gsl-config."
-     else
-       if test -f conf.gsltest ; then
-        :
-       else
-          echo "*** Could not run GSL test program, checking why..."
-          CFLAGS="$CFLAGS $GSL_CFLAGS"
-          LIBS="$LIBS $GSL_LIBS"
-          AC_TRY_LINK([
-#include <stdio.h>
-],      [ return 0; ],
-        [ echo "*** The test program compiled, but did not run. This usually means"
-          echo "*** that the run-time linker is not finding GSL or finding the wrong"
-          echo "*** version of GSL. If it is not finding GSL, you'll need to set your"
-          echo "*** LD_LIBRARY_PATH environment variable, or edit /etc/ld.so.conf to point"
-          echo "*** to the installed location  Also, make sure you have run ldconfig if that"
-          echo "*** is required on your system"
-	  echo "***"
-          echo "*** If you have an old version installed, it is best to remove it, although"
-          echo "*** you may also be able to get things to work by modifying LD_LIBRARY_PATH"],
-        [ echo "*** The test program failed to compile or link. See the file config.log for the"
-          echo "*** exact error that occured. This usually means GSL was incorrectly installed"
-          echo "*** or that you have moved GSL since it was installed. In the latter case, you"
-          echo "*** may want to edit the gsl-config script: $GSL_CONFIG" ])
-          CFLAGS="$ac_save_CFLAGS"
-          LIBS="$ac_save_LIBS"
-       fi
-     fi
-#     GSL_CFLAGS=""
-#     GSL_LIBS=""
-     ifelse([$3], , :, [$3])
-  fi
-  AC_SUBST(GSL_CFLAGS)
-  AC_SUBST(GSL_LIBS)
-  rm -f conf.gsltest
-])
-
-AU_ALIAS([AM_PATH_GSL], [AX_PATH_GSL])
diff --git a/mex/build/matlab/configure.ac b/mex/build/matlab/configure.ac
index a209085b9ea0de7e5121809db307f045e07c1183..66f3a7ba2f0f8d56140228ec99b6ca805eeb40aa 100644
--- a/mex/build/matlab/configure.ac
+++ b/mex/build/matlab/configure.ac
@@ -72,8 +72,8 @@ AC_LANG_POP([C++])
 AC_CHECK_LIB([dl], [dlopen], [LIBADD_DLOPEN="-ldl"], [])
 AC_SUBST([LIBADD_DLOPEN])
 # Check for GSL, needed by SWZ
-AX_PATH_GSL
-AM_CONDITIONAL([HAVE_GSL], [test "x$no_gsl" = "x"])
+AX_GSL
+AM_CONDITIONAL([HAVE_GSL], [test "x$has_gsl" = "xyes"])
 
 AM_CONDITIONAL([DO_SOMETHING], [test "x$ax_enable_matlab" = "xyes" -a "x$ax_matlab_version_ok" = "xyes" -a "x$ax_mexopts_ok" = "xyes"])
 
@@ -87,7 +87,7 @@ else
   BUILD_MEX_MATLAB="no (missing MATLAB, or unknown version, or unknown architecture)"
 fi
 
-if test "x$ax_enable_matlab" = "xyes" -a "x$ax_matlab_version_ok" = "xyes" -a "x$ax_mexopts_ok" = "xyes" -a "x$no_gsl" = "x"; then
+if test "x$ax_enable_matlab" = "xyes" -a "x$ax_matlab_version_ok" = "xyes" -a "x$ax_mexopts_ok" = "xyes" -a "x$has_gsl" = "xyes"; then
    BUILD_SWZ_MEX_MATLAB="yes"
 else
    BUILD_SWZ_MEX_MATLAB="no (missing GSL)"
diff --git a/mex/build/octave/configure.ac b/mex/build/octave/configure.ac
index 556f56098c99477fdf24a109d2189ca8906f2170..0ceee1e09d17b22e417f577b008caa26114e2f53 100644
--- a/mex/build/octave/configure.ac
+++ b/mex/build/octave/configure.ac
@@ -53,8 +53,8 @@ AC_LANG_POP([C++])
 AC_CHECK_LIB([dl], [dlopen], [LIBADD_DLOPEN="-ldl"], [])
 AC_SUBST([LIBADD_DLOPEN])
 # Check for GSL, needed by SWZ
-AX_PATH_GSL
-AM_CONDITIONAL([HAVE_GSL], [test "x$no_gsl" = "x"])
+AX_GSL
+AM_CONDITIONAL([HAVE_GSL], [test "x$has_gsl" = "xyes"])
 
 AM_CONDITIONAL([DO_SOMETHING], [test "x$MKOCTFILE" != "x"])
 
@@ -68,7 +68,7 @@ else
   BUILD_MEX_OCTAVE="no (missing mkoctfile)"
 fi
 
-if test "x$MKOCTFILE" != "x" -a "x$no_gsl" = "x"; then
+if test "x$MKOCTFILE" != "x" -a "x$has_gsl" = "xyes"; then
    BUILD_SWZ_MEX_OCTAVE="yes"
 else
    BUILD_SWZ_MEX_OCTAVE="no (missing GSL)"
diff --git a/mex/build/swz.am b/mex/build/swz.am
index 5c61a204dac89f984b555f8ad84d65b9a8e61d75..80925a984cc3b41b25eac08b0bfa06dea396a8b4 100644
--- a/mex/build/swz.am
+++ b/mex/build/swz.am
@@ -4,9 +4,10 @@ SWZ_SRC_DIRS = $(SWZ_SRC_BASEDIR)/utilities/TZCcode $(SWZ_SRC_BASEDIR)/utilities
 
 vpath %.c $(SWZ_SRC_DIRS)
 
-CPPFLAGS += -DINTELCMATHLIBRARY $(GSL_CFLAGS) -I$(SWZ_SRC_BASEDIR)/utilities/TZCcode -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/matrix -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/error -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/arrays -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/ascii -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/stat -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/spherical -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/sort -I$(SWZ_SRC_BASEDIR)/sbvar/switching -I$(SWZ_SRC_BASEDIR)/sbvar/var -I$(SWZ_SRC_BASEDIR)/mex
+CPPFLAGS += -DINTELCMATHLIBRARY $(GSL_CPPFLAGS) -I$(SWZ_SRC_BASEDIR)/utilities/TZCcode -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/matrix -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/error -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/arrays -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/ascii -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/stat -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/spherical -I$(SWZ_SRC_BASEDIR)/utilities/DWCcode/sort -I$(SWZ_SRC_BASEDIR)/sbvar/switching -I$(SWZ_SRC_BASEDIR)/sbvar/var -I$(SWZ_SRC_BASEDIR)/mex
 
 LIBS += $(GSL_LIBS)
+LDFLAGS += $(GSL_LDFLAGS)
 
 noinst_PROGRAMS = mex_sbvar_init_file mex_sbvar_estimation mex_sbvar_mhm_1 mex_sbvar_mhm_2 mex_sbvar_probabilities mex_sbvar_draws