diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index e29f91d917173fe5623df33172547da077e91c49..1ef6a6749ecc7f458de15f5eb07fe892d259c8b1 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -943,8 +943,8 @@ DynamicModel::writeDynamicJuliaFile(const string &basename) const
   writeToFileIfModified(output, basename + "Dynamic.jl");
 }
 
-filesystem::path
-DynamicModel::writeDynamicCFile(const string &basename) const
+void
+DynamicModel::writeDynamicCFile(const string &basename, const string &mexext, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const
 {
   string filename = basename + "/model/src/dynamic.c";
 
@@ -1074,7 +1074,7 @@ DynamicModel::writeDynamicCFile(const string &basename) const
 
   output.close();
 
-  return filename;
+  compileMEX(basename, "dynamic", mexext, { filename }, matlabroot, dynareroot);
 }
 
 string
@@ -1144,8 +1144,8 @@ DynamicModel::writeDynamicBlockMFile(const string &basename) const
   output.close();
 }
 
-filesystem::path
-DynamicModel::writeDynamicBlockCFile(const string &basename) const
+void
+DynamicModel::writeDynamicBlockCFile(const string &basename, vector<filesystem::path> per_block_src_files, const string &mexext, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const
 {
   string filename = basename + "/model/src/dynamic.c";
 
@@ -1228,7 +1228,8 @@ DynamicModel::writeDynamicBlockCFile(const string &basename) const
 
   output.close();
 
-  return filename;
+  per_block_src_files.push_back(filename);
+  compileMEX(basename, "dynamic", mexext, per_block_src_files, matlabroot, dynareroot);
 }
 
 void
@@ -3606,9 +3607,8 @@ DynamicModel::writeDynamicFile(const string &basename, bool block, bool use_dll,
 
       if (use_dll)
         {
-          auto src_files { writeDynamicPerBlockCFiles(basename) };
-          src_files.emplace_back(writeDynamicBlockCFile(basename));
-          compileMEX(basename, "dynamic", mexext, src_files, matlabroot, dynareroot);
+          auto per_block_src_files { writeDynamicPerBlockCFiles(basename) };
+          writeDynamicBlockCFile(basename, move(per_block_src_files), mexext, matlabroot, dynareroot);
         }
       else if (julia)
         {
@@ -3626,10 +3626,7 @@ DynamicModel::writeDynamicFile(const string &basename, bool block, bool use_dll,
       writeDynamicBytecode(basename);
 
       if (use_dll)
-        {
-          auto src_file { writeDynamicCFile(basename) };
-          compileMEX(basename, "dynamic", mexext, { src_file }, matlabroot, dynareroot);
-        }
+        writeDynamicCFile(basename, mexext, matlabroot, dynareroot);
       else if (julia)
         writeDynamicJuliaFile(basename);
       else
diff --git a/src/DynamicModel.hh b/src/DynamicModel.hh
index c64713aa5a1d3f89aff36804141ffdfbbeafa962..341a4e62c3d7fb4c382f60e3341bf0b96aad677a 100644
--- a/src/DynamicModel.hh
+++ b/src/DynamicModel.hh
@@ -121,14 +121,13 @@ private:
   void writeDynamicMFile(const string &basename) const;
   //! Writes dynamic model file (Julia version)
   void writeDynamicJuliaFile(const string &basename) const;
-  //! Writes dynamic model file (C version)
-  // Returns the path to the generated C source file
-  filesystem::path writeDynamicCFile(const string &basename) const;
+  // Writes and compiles dynamic model file (C version)
+  void writeDynamicCFile(const string &basename, const string &mexext, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const;
   //! Writes the main dynamic function of block decomposed model (MATLAB version)
   void writeDynamicBlockMFile(const string &basename) const;
-  //! Writes the main dynamic function of block decomposed model (C version)
-  // Returns the path to the generated C source file
-  filesystem::path writeDynamicBlockCFile(const string &basename) const;
+  /* Writes the main dynamic functions of block decomposed model (C version),
+     then compiles it with the per-block functions into a single MEX */
+  void writeDynamicBlockCFile(const string &basename, vector<filesystem::path> per_block_src_files, const string &mexext, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const;
   /* Computes the number of nonzero elements in deterministic Jacobian of
      block-decomposed model */
   int nzeDeterministicJacobianForBlock(int blk) const;
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index 5d32589447a3fa2ecb365729158a9a1687ac2070..ba1adfa07d205ca3f8fd17c4f3735f35627bf45e 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -603,8 +603,8 @@ StaticModel::writeStaticMCompatFile(const string &basename) const
   output.close();
 }
 
-filesystem::path
-StaticModel::writeStaticCFile(const string &basename) const
+void
+StaticModel::writeStaticCFile(const string &basename, const string &mexext, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const
 {
   // Writing comments and function definition command
   string filename{basename + "/model/src/static.c"};
@@ -713,7 +713,7 @@ StaticModel::writeStaticCFile(const string &basename) const
 
   output.close();
 
-  return filename;
+  compileMEX(basename, "static", mexext, { filename }, matlabroot, dynareroot);
 }
 
 void
@@ -959,9 +959,8 @@ StaticModel::writeStaticFile(const string &basename, bool block, bool use_dll, c
 
       if (use_dll)
         {
-          auto src_files { writeStaticPerBlockCFiles(basename) };
-          src_files.emplace_back(writeStaticBlockCFile(basename));
-          compileMEX(basename, "static", mexext, src_files, matlabroot, dynareroot);
+          auto per_block_src_files { writeStaticPerBlockCFiles(basename) };
+          writeStaticBlockCFile(basename, move(per_block_src_files), mexext, matlabroot, dynareroot);
         }
       else if (julia)
         {
@@ -979,10 +978,7 @@ StaticModel::writeStaticFile(const string &basename, bool block, bool use_dll, c
       writeStaticBytecode(basename);
 
       if (use_dll)
-        {
-          auto src_file { writeStaticCFile(basename) };
-          compileMEX(basename, "static", mexext, { src_file }, matlabroot, dynareroot);
-        }
+        writeStaticCFile(basename, mexext, matlabroot, dynareroot);
       else if (julia)
         writeStaticJuliaFile(basename);
       else // M-files
@@ -1036,8 +1032,8 @@ StaticModel::writeStaticBlockMFile(const string &basename) const
   output.close();
 }
 
-filesystem::path
-StaticModel::writeStaticBlockCFile(const string &basename) const
+void
+StaticModel::writeStaticBlockCFile(const string &basename, vector<filesystem::path> per_block_src_files, const string &mexext, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const
 {
   string filename = basename + "/model/src/static.c";
 
@@ -1108,7 +1104,8 @@ StaticModel::writeStaticBlockCFile(const string &basename) const
          << "}" << endl;
   output.close();
 
-  return filename;
+  per_block_src_files.push_back(filename);
+  compileMEX(basename, "static", mexext, per_block_src_files, matlabroot, dynareroot);
 }
 
 void
diff --git a/src/StaticModel.hh b/src/StaticModel.hh
index d76202d10e700d4e964528971b8fccf9d8b9be37..ae64f9df568c4b8da0056ce668e650a4dd72c1d7 100644
--- a/src/StaticModel.hh
+++ b/src/StaticModel.hh
@@ -37,9 +37,8 @@ private:
   //! Writes static model file (standard Matlab version)
   void writeStaticMFile(const string &basename) const;
 
-  //! Writes static model file (C version)
-  // Returns the path to the generated C source file
-  filesystem::path writeStaticCFile(const string &basename) const;
+  // Writes and compiles static model file (C version)
+  void writeStaticCFile(const string &basename, const string &mexext, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const;
 
   //! Writes static model file (Julia version)
   void writeStaticJuliaFile(const string &basename) const;
@@ -47,9 +46,9 @@ private:
   //! Writes the main static function of block decomposed model (MATLAB version)
   void writeStaticBlockMFile(const string &basename) const;
 
-  //! Writes the main static function of block decomposed model (C version)
-  // Returns the path to the generated C source file
-  filesystem::path writeStaticBlockCFile(const string &basename) const;
+  /* Writes the main static functions of block decomposed model (C version),
+     then compiles it with the per-block functions into a single MEX */
+  void writeStaticBlockCFile(const string &basename, vector<filesystem::path> per_block_src_files, const string &mexext, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const;
 
   //! Helper for writing a per-block static file of block decomposed model
   template<ExprNodeOutputType output_type>