MSVC: compat: dirname() from <libgen.h>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 272dd00..dd29a32 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -367,7 +367,7 @@
     include_directories(${COMPAT_POSIX_INCLUDES})
 
     find_package(pthreads REQUIRED)
-    target_link_libraries(yang PThreads4W::PThreads4W)
+    target_link_libraries(yang PThreads4W::PThreads4W shlwapi.lib)
 endif()
 
 set_target_properties(yang PROPERTIES VERSION ${LIBYANG_SOVERSION_FULL} SOVERSION ${LIBYANG_SOVERSION})
diff --git a/CMakeModules/UseCompat.cmake b/CMakeModules/UseCompat.cmake
index 753b10d..eb4b103 100644
--- a/CMakeModules/UseCompat.cmake
+++ b/CMakeModules/UseCompat.cmake
@@ -55,6 +55,7 @@
     check_symbol_exists(localtime_r "time.h" HAVE_LOCALTIME_R)
     check_symbol_exists(strptime "time.h" HAVE_STRPTIME)
     check_symbol_exists(mmap "sys/mman.h" HAVE_MMAP)
+    check_symbol_exists(dirname "libgen.h" HAVE_DIRNAME)
 
     unset(CMAKE_REQUIRED_DEFINITIONS)
     unset(CMAKE_REQUIRED_LIBRARIES)
diff --git a/compat/compat.c b/compat/compat.c
index ea8eb03..6763413 100644
--- a/compat/compat.c
+++ b/compat/compat.c
@@ -289,3 +289,18 @@
 #error No localtime_r() implementation for this platform is available.
 #endif
 #endif
+
+#ifndef HAVE_DIRNAME
+#ifdef _WIN32
+#include <shlwapi.h>
+char *
+dirname(char *path)
+{
+    PathRemoveFileSpecA(path);
+    return path;
+}
+
+#else
+#error No dirname() implementation for this platform is available.
+#endif
+#endif
diff --git a/compat/compat.h.in b/compat/compat.h.in
index 6672fcf..1c19b98 100644
--- a/compat/compat.h.in
+++ b/compat/compat.h.in
@@ -65,6 +65,7 @@
 #cmakedefine HAVE_LOCALTIME_R
 #cmakedefine HAVE_STRPTIME
 #cmakedefine HAVE_MMAP
+#cmakedefine HAVE_DIRNAME
 
 #ifndef bswap64
 #define bswap64(val) \
diff --git a/compat/posix-shims/libgen.h b/compat/posix-shims/libgen.h
new file mode 100644
index 0000000..014bb07
--- /dev/null
+++ b/compat/posix-shims/libgen.h
@@ -0,0 +1 @@
+char *dirname(char *path);