diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc
index 1ef6a6749ecc7f458de15f5eb07fe892d259c8b1..742defe69a8c07e724651986a00a69040a9a92c7 100644
--- a/src/DynamicModel.cc
+++ b/src/DynamicModel.cc
@@ -1074,7 +1074,7 @@ DynamicModel::writeDynamicCFile(const string &basename, const string &mexext, co
 
   output.close();
 
-  compileMEX(basename, "dynamic", mexext, { filename }, matlabroot, dynareroot);
+  compileMEX("+" + basename, "dynamic", mexext, { filename }, matlabroot, dynareroot);
 }
 
 string
@@ -1229,7 +1229,7 @@ DynamicModel::writeDynamicBlockCFile(const string &basename, vector<filesystem::
   output.close();
 
   per_block_src_files.push_back(filename);
-  compileMEX(basename, "dynamic", mexext, per_block_src_files, matlabroot, dynareroot);
+  compileMEX("+" + basename, "dynamic", mexext, per_block_src_files, matlabroot, dynareroot);
 }
 
 void
diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index e375c68468d0a5548536769a1de0cfbbfd3fe1fd..59172a84af794ffbbd1bb27ed975b7164a0bdd40 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -1619,7 +1619,7 @@ ModelTree::findGccOnMacos(const string &mexext)
 #endif
 
 void
-ModelTree::compileMEX(const string &basename, const string &funcname, const string &mexext, const vector<filesystem::path> &src_files, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const
+ModelTree::compileMEX(const filesystem::path &output_dir, const string &funcname, const string &mexext, const vector<filesystem::path> &src_files, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const
 {
   const string opt_flags = "-O3 -g0 --param ira-max-conflict-table-size=1 -fno-forward-propagate -fno-gcse -fno-dce -fno-dse -fno-tree-fre -fno-tree-pre -fno-tree-cselim -fno-tree-dse -fno-tree-dce -fno-tree-pta -fno-gcse-after-reload";
 
@@ -1704,8 +1704,7 @@ ModelTree::compileMEX(const string &basename, const string &funcname, const stri
         }
     }
 
-  filesystem::path mex_dir{"+" + basename};
-  filesystem::path binary{mex_dir / (funcname + "." + mexext)};
+  filesystem::path binary{output_dir / (funcname + "." + mexext)};
 
   ostringstream cmd;
 
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 68887054021f26a37a00eff6bc707190d1fa1baa..bc1e7224142368fd575aca6ec3e4db039610c9dd 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -490,7 +490,7 @@ private:
      files, those get compiled in separate threads; this could however
      require implementing a scheduler, so as to not run more threads than
      there are logical cores. */
-  void compileMEX(const string &basename, const string &funcname, const string &mexext, const vector<filesystem::path> &src_files, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const;
+  void compileMEX(const filesystem::path &output_dir, const string &funcname, const string &mexext, const vector<filesystem::path> &src_files, const filesystem::path &matlabroot, const filesystem::path &dynareroot) const;
 
 public:
   ModelTree(SymbolTable &symbol_table_arg,
diff --git a/src/StaticModel.cc b/src/StaticModel.cc
index ba1adfa07d205ca3f8fd17c4f3735f35627bf45e..2f02ee82f374f5537199f0905619d02ca55476cb 100644
--- a/src/StaticModel.cc
+++ b/src/StaticModel.cc
@@ -713,7 +713,7 @@ StaticModel::writeStaticCFile(const string &basename, const string &mexext, cons
 
   output.close();
 
-  compileMEX(basename, "static", mexext, { filename }, matlabroot, dynareroot);
+  compileMEX("+" + basename, "static", mexext, { filename }, matlabroot, dynareroot);
 }
 
 void
@@ -1105,7 +1105,7 @@ StaticModel::writeStaticBlockCFile(const string &basename, vector<filesystem::pa
   output.close();
 
   per_block_src_files.push_back(filename);
-  compileMEX(basename, "static", mexext, per_block_src_files, matlabroot, dynareroot);
+  compileMEX("+" + basename, "static", mexext, per_block_src_files, matlabroot, dynareroot);
 }
 
 void