diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index 51060f45385f0dac00ba7d12d0deb9ee6ed45d40..1986485b059bc8f3b713bce528ec56e823a36a11 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -44,7 +44,7 @@
 condition_variable_any ModelTree::mex_compilation_cv;
 mutex ModelTree::mex_compilation_mut;
 vector<tuple<filesystem::path, set<filesystem::path>, string>> ModelTree::mex_compilation_queue;
-set<filesystem::path> ModelTree::mex_compilation_done;
+set<filesystem::path> ModelTree::mex_compilation_ongoing, ModelTree::mex_compilation_done, ModelTree::mex_compilation_failed;
 vector<jthread> ModelTree::mex_compilation_workers;
 
 void
@@ -1955,6 +1955,7 @@ ModelTree::initializeMEXCompilationWorkers(int numworkers)
               output = get<0>(*it);
               cmd = get<2>(*it);
               mex_compilation_queue.erase(it);
+              mex_compilation_ongoing.insert(output);
               return true;
             }
         return false;
@@ -1964,13 +1965,13 @@ ModelTree::initializeMEXCompilationWorkers(int numworkers)
         if (mex_compilation_cv.wait(lk, stoken, pick_job))
           {
             lk.unlock();
-            if (system(cmd.c_str()))
-              {
-                cerr << "Compilation failed" << endl;
-                exit(EXIT_FAILURE);
-              }
+            int r { system(cmd.c_str()) };
             lk.lock();
-            mex_compilation_done.insert(output);
+            mex_compilation_ongoing.erase(output);
+            if (r)
+              mex_compilation_failed.insert(output);
+            else
+              mex_compilation_done.insert(output);
             /* The object just compiled may be a prerequisite for several
                other objects, so notify all waiting workers. Also needed to
                notify the main thread when in
@@ -1984,7 +1985,18 @@ void
 ModelTree::waitForMEXCompilationWorkers()
 {
   unique_lock<mutex> lk {mex_compilation_mut};
-  mex_compilation_cv.wait(lk, [] { return mex_compilation_queue.empty(); });
+  mex_compilation_cv.wait(lk, [] {
+    return (mex_compilation_queue.empty() && mex_compilation_ongoing.empty())
+      || !mex_compilation_failed.empty(); });
+  if (!mex_compilation_failed.empty())
+    {
+      cerr << "Compilation failed for: ";
+      for (const auto &p : mex_compilation_failed)
+        cerr << p.string() << " ";
+      cerr << endl;
+      lk.unlock(); // So that threads can process their stoken
+      exit(EXIT_FAILURE);
+    }
 }
 
 void
diff --git a/src/ModelTree.hh b/src/ModelTree.hh
index 84a5d60b4ce70e1f0b532a17a40171217521f41b..a68bb22b5f4387aeecf82638564b81de14a7ac56 100644
--- a/src/ModelTree.hh
+++ b/src/ModelTree.hh
@@ -439,8 +439,12 @@ private:
   /* Object/MEX files waiting to be compiled (with their prerequisites as 2nd
      element and compilation command as the 3rd element) */
   static vector<tuple<filesystem::path, set<filesystem::path>, string>> mex_compilation_queue;
-  // Object/MEX files already compiled
+  // Object/MEX files in the process of being compiled
+  static set<filesystem::path> mex_compilation_ongoing;
+  // Object/MEX files already compiled successfully
   static set<filesystem::path> mex_compilation_done;
+  // Object/MEX files whose compilation failed
+  static set<filesystem::path> mex_compilation_failed;
 
   /* Compute a pseudo-Jacobian whose all elements are either zero or one,
      depending on whether the variable symbolically appears in the equation. If