diff --git a/mex/sources/k_order_perturbation/dynamic_dll.cpp b/mex/sources/k_order_perturbation/dynamic_dll.cpp
index 3e1f75fb218ecfd6641497cc53b2cf87726a9be6..4ee119dab5bd3d2f8c59fe3ff5e5a4f9000c3861 100644
--- a/mex/sources/k_order_perturbation/dynamic_dll.cpp
+++ b/mex/sources/k_order_perturbation/dynamic_dll.cpp
@@ -75,7 +75,7 @@ DynamicModelDLL::DynamicModelDLL(const string &modName, const int y_length, cons
 
 DynamicModelDLL::~DynamicModelDLL()
 {
-#ifdef _WIN32
+#if !defined(__CYGWIN32__) && !defined(_WIN32)
   FreeLibrary(dynamicHinstance);
 #else
   dlclose(dynamicHinstance);