diff --git a/cmake/modules/CheckStdFilesystem.cmake b/cmake/modules/CheckStdFilesystem.cmake
new file mode 100644
--- /dev/null
+++ b/cmake/modules/CheckStdFilesystem.cmake
@@ -0,0 +1,37 @@
+# Copyright (c) 2022 The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
+
+# GCC 8.x (libstdc++) requires -lstdc++fs
+# Clang 8.x (libc++) requires -lc++fs
+
+function(check_std_filesystem)
+
+	set(source "
+#include <filesystem>
+int main() {
+    (void)std::filesystem::current_path().root_name();
+}
+	")
+
+	include(CheckCXXSourceCompiles)
+	check_cxx_source_compiles("${source}" STD_FILESYSTEM_WORKS_WITHOUT_LINK)
+	if(STD_FILESYSTEM_WORKS_WITHOUT_LINK)
+		return()
+	endif()
+
+	add_library(std_filesystem INTERFACE)
+	set(CMAKE_REQUIRED_LIBRARIES "stdc++fs")
+	check_cxx_source_compiles("${source}" STD_FILESYSTEM_NEEDS_LINK_TO_STDCXXFS)
+	if(STD_FILESYSTEM_NEEDS_LINK_TO_STDCXXFS)
+		target_link_libraries(std_filesystem INTERFACE ${CMAKE_REQUIRED_LIBRARIES})
+		return()
+	endif()
+	set(CMAKE_REQUIRED_LIBRARIES "c++fs")
+	check_cxx_source_compiles("${source}" STD_FILESYSTEM_NEEDS_LINK_TO_CXXFS)
+	if(STD_FILESYSTEM_NEEDS_LINK_TO_CXXFS)
+		target_link_libraries(std_filesystem INTERFACE ${CMAKE_REQUIRED_LIBRARIES})
+		return()
+	endif()
+	message(FATAL_ERROR "Cannot figure out how to use std::filesystem.")
+endfunction()
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -309,7 +309,6 @@
 	endif()
 endif()
 
-
 # Make sure that all the global compiler and linker flags are set BEFORE
 # including the libraries so they apply as needed.
 
@@ -464,6 +463,12 @@
 # Make sure boost uses std::atomic (it doesn't before 1.63)
 target_compile_definitions(util PUBLIC BOOST_SP_USE_STD_ATOMIC BOOST_AC_USE_STD_ATOMIC)
 
+include(CheckStdFilesystem)
+check_std_filesystem()
+if(TARGET std_filesystem)
+  target_link_libraries(util PUBLIC std_filesystem)
+endif()
+
 function(add_network_sources NETWORK_SOURCES)
 	set(NETWORK_DIR abc)