diff --git a/others/cpp/dynare_cpp_driver.hh b/others/cpp/dynare_cpp_driver.hh
index 581cdd6eae7f38d08155081a674516fbcee84b75..9e39ee96e5c06e13154cf414a19f1059da9cf633 100644
--- a/others/cpp/dynare_cpp_driver.hh
+++ b/others/cpp/dynare_cpp_driver.hh
@@ -256,7 +256,6 @@ public:
              vector<int> predetermined_variables_arg,
              vector<int> varobs_arg,
              vector<int> NNZDerivatives_arg);
-  ~DynareInfo();
 
   inline void addMarkovSwitching(MarkovSwitching *ms) { markov_switching_vector.push_back(ms); };
 
diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index 6c506c400a8b495fcc287422546e884c35cbcf91..1a6f954b3db23095dd993c4add3a3078f17bc03c 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -4431,6 +4431,55 @@ DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_d
     }
 }
 
+void
+DynamicModel::writeResidualsC(const string &basename, bool cuda) const
+{
+  string filename = basename + "_residuals.c";
+  ofstream mDynamicModelFile, mDynamicMexFile;
+
+  mDynamicModelFile.open(filename.c_str(), ios::out | ios::binary);
+  if (!mDynamicModelFile.is_open())
+    {
+      cerr << "Error: Can't open file " << filename << " for writing" << endl;
+      exit(EXIT_FAILURE);
+    }
+  mDynamicModelFile << "/*" << endl
+                    << " * " << filename << " : Computes residuals of the model for Dynare" << endl
+                    << " *" << endl
+                    << " * Warning : this file is generated automatically by Dynare" << endl
+                    << " *           from model " << basename << "(.mod)" << endl
+                    << " */" << endl
+                    << "#include <math.h>" << endl;
+
+  mDynamicModelFile << "#include <stdlib.h>" << endl;
+
+  mDynamicModelFile << "#define max(a, b) (((a) > (b)) ? (a) : (b))" << endl
+                    << "#define min(a, b) (((a) > (b)) ? (b) : (a))" << endl;
+
+  // Write function definition if oPowerDeriv is used
+  // even for residuals if doing Ramsey
+  writePowerDerivCHeader(mDynamicModelFile);
+
+  mDynamicModelFile << "void Residuals(const double *y, double *x, int nb_row_x, double *params, double *steady_state, int it_, double *residual)" << endl
+                    << "{" << endl;
+
+  // this is always empty here, but needed by d1->writeOutput
+  deriv_node_temp_terms_t tef_terms;
+
+  ostringstream model_output;    // Used for storing model equations
+  writeModelEquations(model_output, oCDynamicModel);
+
+  mDynamicModelFile << "  double lhs, rhs;" << endl
+                    << endl
+                    << "  /* Residual equations */" << endl
+                    << model_output.str()
+		    << "}" << endl;
+
+  writePowerDeriv(mDynamicModelFile, true);
+  mDynamicModelFile.close();
+
+}
+
 void
 DynamicModel::writeFirstDerivativesC(const string &basename, bool cuda) const
 {
@@ -4475,14 +4524,16 @@ DynamicModel::writeFirstDerivativesC(const string &basename, bool cuda) const
 
       jacobianHelper(mDynamicModelFile, eq, getDynJacobianCol(var), oCDynamicModel);
       mDynamicModelFile << "=";
-      // oCstaticModel makes reference to the static variables
-      d1->writeOutput(mDynamicModelFile, oCStaticModel, temporary_terms, tef_terms);
+      // oCStaticModel makes reference to the static variables
+      // oCDynamicModel makes reference to the dynamic variables
+      d1->writeOutput(mDynamicModelFile, oCDynamicModel, temporary_terms, tef_terms);
       mDynamicModelFile << ";" << endl;
     }
 
   mDynamicModelFile << "}" << endl;
 
-  writePowerDeriv(mDynamicModelFile, true);
+  // already written in writeResidualsC()
+  // writePowerDeriv(mDynamicModelFile, true);
   mDynamicModelFile.close();
 
 }
diff --git a/preprocessor/DynamicModel.hh b/preprocessor/DynamicModel.hh
index 6f1eefd7e900a53d1f732164248a086ba8a8d79e..a9655220e11aec9930429fb77025e1ee9ea11e5e 100644
--- a/preprocessor/DynamicModel.hh
+++ b/preprocessor/DynamicModel.hh
@@ -476,6 +476,8 @@ public:
   void writeCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const;
   //! Writes model initialization and lead/lag incidence matrix to Cpp output
   void writeCCOutput(ostream &output, const string &basename, bool block, bool byte_code, bool use_dll, int order, bool estimation_present) const;
+  //! Writes C file containing residuals
+  void writeResidualsC(const string &basename, bool cuda) const;
   //! Writes C file containing first order derivatives of model evaluated at steady state
   void writeFirstDerivativesC(const string &basename, bool cuda) const;
   //! Writes C file containing second order derivatives of model evaluated at steady state (compressed sparse column)
diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc
index 1b7f286b028e60ff8700429f095fa652897b7dfb..a3de63a7ed4ee3cfdbeb0a2b90a6bb4bceeb1a5d 100644
--- a/preprocessor/ModFile.cc
+++ b/preprocessor/ModFile.cc
@@ -865,7 +865,7 @@ ModFile::writeExternalFilesC(const string &basename, FileOutputType output) cons
   //  static_model.writeParamsDerivativesFileC(basename, cuda);
   //  static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda);
 
-  // dynamic_model.writeResidualsC(basename, cuda);
+  dynamic_model.writeResidualsC(basename, cuda);
   // dynamic_model.writeParamsDerivativesFileC(basename, cuda);
   dynamic_model.writeFirstDerivativesC(basename, cuda);
 
@@ -971,6 +971,7 @@ ModFile::writeExternalFilesCC(const string &basename, FileOutputType output) con
 
   // dynamic_model.writeResidualsC(basename, cuda);
   // dynamic_model.writeParamsDerivativesFileC(basename, cuda);
+  dynamic_model.writeResidualsC(basename, cuda);
   dynamic_model.writeFirstDerivativesC(basename, cuda);
 
   if (output == second)