diff --git a/mex/build/dynare_simul_.am b/mex/build/dynare_simul_.am
new file mode 100644
index 0000000000000000000000000000000000000000..672adb696e7f4e58d95bb810b89da61c6f1def6c
--- /dev/null
+++ b/mex/build/dynare_simul_.am
@@ -0,0 +1,10 @@
+vpath %.cpp $(top_srcdir)/../../../dynare++/extern/matlab
+
+noinst_PROGRAMS = dynare_simul_
+
+# Can't use gensylv_CPPFLAGS, because it interacts badly with VPATH
+CPPFLAGS += -I$(top_srcdir)/../../../dynare++/sylv/cc -I$(top_srcdir)/../../../dynare++/tl/cc -I$(top_srcdir)/../../../dynare++/kord -I$(top_srcdir)/../../sources
+
+dynare_simul__LDADD = ../libdynare++/libdynare++.a
+
+nodist_dynare_simul__SOURCES = dynare_simul.cpp
diff --git a/mex/build/gensylv.am b/mex/build/gensylv.am
index e09be2b7aa65b4d5def9e4f31e12b89e3495d5d5..ebedd6594d30b920b537c1a17b4acf134f1dc0d9 100644
--- a/mex/build/gensylv.am
+++ b/mex/build/gensylv.am
@@ -1,45 +1,10 @@
-vpath %.cpp $(top_srcdir)/../../../dynare++/sylv/cc $(top_srcdir)/../../../dynare++/sylv/matlab
-vpath %.h $(top_srcdir)/../../../dynare++/sylv/cc
+vpath %.cpp $(top_srcdir)/../../../dynare++/sylv/matlab
 
 noinst_PROGRAMS = gensylv
 
 # Can't use gensylv_CPPFLAGS, because it interacts badly with VPATH
 CPPFLAGS += -I$(top_srcdir)/../../../dynare++/sylv/cc -I$(top_srcdir)/../../sources
 
-nodist_gensylv_SOURCES = \
-	gensylv.cpp \
-	BlockDiagonal.cpp \
-	GeneralMatrix.cpp \
-	GeneralSylvester.cpp \
-	IterativeSylvester.cpp \
-	KronUtils.cpp \
-	KronVector.cpp \
-	QuasiTriangular.cpp \
-	QuasiTriangularZero.cpp \
-	SchurDecomp.cpp \
-	SchurDecompEig.cpp \
-	SimilarityDecomp.cpp \
-	SylvException.cpp \
-	SylvMatrix.cpp \
-	SylvMemory.cpp \
-	SylvParams.cpp \
-	TriangularSylvester.cpp \
-	Vector.cpp \
-	BlockDiagonal.h \
-	GeneralMatrix.h \
-	GeneralSylvester.h \
-	IterativeSylvester.h \
-	KronUtils.h \
-	KronVector.h \
-	QuasiTriangular.h \
-	QuasiTriangularZero.h \
-	SchurDecomp.h \
-	SchurDecompEig.h \
-	SimilarityDecomp.h \
-	SylvException.h \
-	SylvMatrix.h \
-	SylvMemory.h \
-	SylvParams.h \
-	TriangularSylvester.h \
-	Vector.h \
-	SylvesterSolver.h
+gensylv_LDADD = ../libdynare++/libdynare++.a
+
+nodist_gensylv_SOURCES = gensylv.cpp
diff --git a/mex/build/k_order_perturbation.am b/mex/build/k_order_perturbation.am
index 239e767b21c87e07394a083845a09779164cc335..54d7ded32000c69de5990222fbf9e4e0f8f7dfee 100644
--- a/mex/build/k_order_perturbation.am
+++ b/mex/build/k_order_perturbation.am
@@ -1,4 +1,4 @@
-vpath %.cpp $(top_srcdir)/../../sources/k_order_perturbation $(top_srcdir)/../../../dynare++/sylv/cc $(top_srcdir)/../../../dynare++/tl/cc $(top_srcdir)/../../../dynare++/kord $(top_srcdir)/../../../dynare++/integ/cc $(top_srcdir)/../../../dynare++/src
+vpath %.cpp $(top_srcdir)/../../sources/k_order_perturbation
 
 noinst_PROGRAMS = k_order_perturbation
 
@@ -7,79 +7,11 @@ CPPFLAGS += -I$(top_srcdir)/../../../dynare++/src -I$(top_srcdir)/../../../dynar
 
 CXXFLAGS += $(PTHREAD_CFLAGS)
 
-LIBS += $(PTHREAD_LIBS) $(LIBADD_DLOPEN)
-
-KORD_SRCS = \
-	faa_di_bruno.cpp \
-	korder_stoch.cpp \
-	journal.cpp \
-	decision_rule.cpp \
-	dynamic_model.cpp \
-	random.cpp \
-	first_order.cpp \
-	normal_conjugate.cpp \
-	approximation.cpp \
-	global_check.cpp \
-	korder.cpp
-
-SYLV_SRCS = \
-	IterativeSylvester.cpp \
-	QuasiTriangular.cpp \
-	QuasiTriangularZero.cpp \
-	GeneralMatrix.cpp \
-	GeneralSylvester.cpp \
-	SimilarityDecomp.cpp \
-	SylvException.cpp \
-	SchurDecompEig.cpp \
-	Vector.cpp \
-	TriangularSylvester.cpp \
-	SylvParams.cpp \
-	BlockDiagonal.cpp \
-	KronVector.cpp \
-	SylvMemory.cpp \
-	SymSchurDecomp.cpp \
-	SylvMatrix.cpp \
-	SchurDecomp.cpp \
-	KronUtils.cpp
-
-TL_SRCS = \
-	normal_moments.cpp \
-	int_sequence.cpp \
-	tensor.cpp \
-	ps_tensor.cpp \
-	pyramid_prod2.cpp \
-	equivalence.cpp \
-	fine_container.cpp \
-	kron_prod.cpp \
-	t_polynomial.cpp \
-	symmetry.cpp \
-	stack_container.cpp \
-	twod_matrix.cpp \
-	sparse_tensor.cpp \
-	sthread.cpp \
-	gs_tensor.cpp \
-	pyramid_prod.cpp \
-	fs_tensor.cpp \
-	permutation.cpp \
-	rfs_tensor.cpp \
-	t_container.cpp \
-	tl_static.cpp
-
-INTEG_SRCS = \
-	product.cpp \
-	quadrature.cpp \
-	quasi_mcarlo.cpp \
-	smolyak.cpp \
-	vector_function.cpp
+k_order_perturbation_LDADD = $(PTHREAD_LIBS) $(LIBADD_DLOPEN) ../libdynare++/libdynare++.a
 
 nodist_k_order_perturbation_SOURCES = \
 	k_order_perturbation.cpp \
 	k_ord_dynare.cpp \
 	k_ord_dynare.h \
 	dynamic_dll.cpp \
-	dynamic_dll.h \
-	$(KORD_SRCS) \
-	$(TL_SRCS) \
-	$(SYLV_SRCS) \
-	$(INTEG_SRCS) \
-	nlsolve.cpp
+	dynamic_dll.h
diff --git a/mex/build/libdynare++.am b/mex/build/libdynare++.am
new file mode 100644
index 0000000000000000000000000000000000000000..bbe523012545454c427ee2c0fb728c33ba985e3a
--- /dev/null
+++ b/mex/build/libdynare++.am
@@ -0,0 +1,78 @@
+vpath %.cpp $(top_srcdir)/../../../dynare++/sylv/cc $(top_srcdir)/../../../dynare++/tl/cc $(top_srcdir)/../../../dynare++/kord $(top_srcdir)/../../../dynare++/integ/cc $(top_srcdir)/../../../dynare++/src
+
+noinst_LIBRARIES = libdynare++.a
+
+# Can't use k_order_perturbation_CPPFLAGS, because it interacts badly with VPATH
+CPPFLAGS += -I$(top_srcdir)/../../../dynare++/src -I$(top_srcdir)/../../../dynare++/kord -I$(top_srcdir)/../../../dynare++/tl/cc -I$(top_srcdir)/../../../dynare++/utils/cc -I$(top_srcdir)/../../../dynare++/sylv/cc -I$(top_srcdir)/../../../dynare++/integ/cc -I$(top_srcdir)/../../sources
+
+CXXFLAGS += $(PTHREAD_CFLAGS)
+
+KORD_SRCS = \
+	faa_di_bruno.cpp \
+	korder_stoch.cpp \
+	journal.cpp \
+	decision_rule.cpp \
+	dynamic_model.cpp \
+	random.cpp \
+	first_order.cpp \
+	normal_conjugate.cpp \
+	approximation.cpp \
+	global_check.cpp \
+	korder.cpp
+
+SYLV_SRCS = \
+	IterativeSylvester.cpp \
+	QuasiTriangular.cpp \
+	QuasiTriangularZero.cpp \
+	GeneralMatrix.cpp \
+	GeneralSylvester.cpp \
+	SimilarityDecomp.cpp \
+	SylvException.cpp \
+	SchurDecompEig.cpp \
+	Vector.cpp \
+	TriangularSylvester.cpp \
+	SylvParams.cpp \
+	BlockDiagonal.cpp \
+	KronVector.cpp \
+	SylvMemory.cpp \
+	SymSchurDecomp.cpp \
+	SylvMatrix.cpp \
+	SchurDecomp.cpp \
+	KronUtils.cpp
+
+TL_SRCS = \
+	normal_moments.cpp \
+	int_sequence.cpp \
+	tensor.cpp \
+	ps_tensor.cpp \
+	pyramid_prod2.cpp \
+	equivalence.cpp \
+	fine_container.cpp \
+	kron_prod.cpp \
+	t_polynomial.cpp \
+	symmetry.cpp \
+	stack_container.cpp \
+	twod_matrix.cpp \
+	sparse_tensor.cpp \
+	sthread.cpp \
+	gs_tensor.cpp \
+	pyramid_prod.cpp \
+	fs_tensor.cpp \
+	permutation.cpp \
+	rfs_tensor.cpp \
+	t_container.cpp \
+	tl_static.cpp
+
+INTEG_SRCS = \
+	product.cpp \
+	quadrature.cpp \
+	quasi_mcarlo.cpp \
+	smolyak.cpp \
+	vector_function.cpp
+
+nodist_libdynare___a_SOURCES = \
+	$(KORD_SRCS) \
+	$(TL_SRCS) \
+	$(SYLV_SRCS) \
+	$(INTEG_SRCS) \
+	nlsolve.cpp
diff --git a/mex/build/matlab/Makefile.am b/mex/build/matlab/Makefile.am
index ac99288bc22daa6d11d15d1855bd17aa38261778..a7bd8029bb370d990a8bb718f0ae1f9b535db2ce 100644
--- a/mex/build/matlab/Makefile.am
+++ b/mex/build/matlab/Makefile.am
@@ -1,7 +1,8 @@
 ACLOCAL_AMFLAGS = -I ../../../m4
 
+# libdynare++ must come before gensylv, k_order_perturbation, dynare_simul_
 if DO_SOMETHING
-SUBDIRS = mjdgges gensylv kronecker bytecode k_order_perturbation
+SUBDIRS = mjdgges kronecker bytecode libdynare++ gensylv k_order_perturbation dynare_simul_
 endif
 
 EXTRA_DIST = mex.def mexFunction-MacOSX.map
diff --git a/mex/build/matlab/configure.ac b/mex/build/matlab/configure.ac
index 22f7f02fbf44275812ece5a0c2bcce35607d25d1..794ff7c831a07dedd1ed20049c28db6b03d30eee 100644
--- a/mex/build/matlab/configure.ac
+++ b/mex/build/matlab/configure.ac
@@ -47,6 +47,7 @@ fi
 
 AC_PROG_CC
 AC_PROG_CXX
+AC_PROG_RANLIB
 AX_PROG_LN_S
 AX_PTHREAD
 # Check for dlopen(), needed by k_order_perturbation DLL
@@ -59,8 +60,10 @@ AC_CONFIG_FILES([Makefile
                  mjdgges/Makefile
                  kronecker/Makefile
                  gensylv/Makefile
+                 libdynare++/Makefile
                  bytecode/Makefile
-                 k_order_perturbation/Makefile])
+                 k_order_perturbation/Makefile
+                 dynare_simul_/Makefile])
 
 if test "x$ax_enable_matlab" = "xyes" -a "x$ax_matlab_version_ok" = "xyes" -a "x$ax_mexopts_ok" = "xyes"; then
   if test x"$ax_pthread_ok" = "xyes"; then
diff --git a/mex/build/matlab/dynare_simul_/Makefile.am b/mex/build/matlab/dynare_simul_/Makefile.am
new file mode 100644
index 0000000000000000000000000000000000000000..b7301c554041b7fbb416f893c0da9c2969712cb7
--- /dev/null
+++ b/mex/build/matlab/dynare_simul_/Makefile.am
@@ -0,0 +1,2 @@
+include ../mex.am
+include ../../dynare_simul_.am
diff --git a/mex/build/matlab/libdynare++/Makefile.am b/mex/build/matlab/libdynare++/Makefile.am
new file mode 100644
index 0000000000000000000000000000000000000000..c1283b13cdd77e87a6a1704503b546e05a267a65
--- /dev/null
+++ b/mex/build/matlab/libdynare++/Makefile.am
@@ -0,0 +1,2 @@
+include ../mex.am
+include ../../libdynare++.am
diff --git a/mex/build/octave/Makefile.am b/mex/build/octave/Makefile.am
index 3bbb966031e0efb481aa5012ef3310baf7202f9e..4e70da5e6bfb5b1648f5843668919b5ca89f627a 100644
--- a/mex/build/octave/Makefile.am
+++ b/mex/build/octave/Makefile.am
@@ -1,5 +1,6 @@
 ACLOCAL_AMFLAGS = -I ../../../m4
 
+# libdynare++ must come before gensylv, k_order_perturbation, dynare_simul_
 if DO_SOMETHING
-SUBDIRS = mjdgges gensylv kronecker bytecode k_order_perturbation
+SUBDIRS = mjdgges kronecker bytecode libdynare++ gensylv k_order_perturbation dynare_simul_
 endif
diff --git a/mex/build/octave/configure.ac b/mex/build/octave/configure.ac
index 6b6fc0f1076cba9301c6d42874fc99df9542b26a..995e942649d49c93ac70484ab8b074ee9ff72a0b 100644
--- a/mex/build/octave/configure.ac
+++ b/mex/build/octave/configure.ac
@@ -33,6 +33,7 @@ fi
 
 AC_PROG_CC
 AC_PROG_CXX
+AC_PROG_RANLIB
 AX_PROG_LN_S
 AX_PTHREAD
 # Check for dlopen(), needed by k_order_perturbation DLL
@@ -63,8 +64,10 @@ Binaries (with "make"):
 AC_CONFIG_FILES([Makefile
                  mjdgges/Makefile
                  kronecker/Makefile
-                 gensylv/Makefile
                  bytecode/Makefile
-                 k_order_perturbation/Makefile])
+                 libdynare++/Makefile
+                 gensylv/Makefile
+                 k_order_perturbation/Makefile
+                 dynare_simul_/Makefile])
 
 AC_OUTPUT
diff --git a/mex/build/octave/dynare_simul_/Makefile.am b/mex/build/octave/dynare_simul_/Makefile.am
new file mode 100644
index 0000000000000000000000000000000000000000..b7301c554041b7fbb416f893c0da9c2969712cb7
--- /dev/null
+++ b/mex/build/octave/dynare_simul_/Makefile.am
@@ -0,0 +1,2 @@
+include ../mex.am
+include ../../dynare_simul_.am
diff --git a/mex/build/octave/libdynare++/Makefile.am b/mex/build/octave/libdynare++/Makefile.am
new file mode 100644
index 0000000000000000000000000000000000000000..c1283b13cdd77e87a6a1704503b546e05a267a65
--- /dev/null
+++ b/mex/build/octave/libdynare++/Makefile.am
@@ -0,0 +1,2 @@
+include ../mex.am
+include ../../libdynare++.am