diff --git a/src/ModelTree.cc b/src/ModelTree.cc
index 1bbcb48c5b84699acb39490955749fbeddc32655..47bef3161eb4992aabf4e942a753a450dcf75308 100644
--- a/src/ModelTree.cc
+++ b/src/ModelTree.cc
@@ -1978,6 +1978,12 @@ ModelTree::compileMEX(const string &basename, const string &funcname, const stri
   ostringstream flags;
   string libs;
 
+  if (matlabroot.empty())
+    {
+      cerr << "ERROR: 'matlabroot' option to preprocessor is not set, needed with 'use_dll'" << endl;
+      exit(EXIT_FAILURE);
+    }
+
   if (mexext == "mex")
     {
       // Octave