diff --git a/CMakeLists.txt b/CMakeLists.txt
index a3db42298460edd7cf8612cdb180e97437b2d03b..90178c7adc58501acd5d9f66e48f0e348d3e7d2e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -8,6 +8,7 @@ set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 include(CTest)
 include(GNUInstallDirs)
+include(CheckIncludeFileCXX)
 set (prefix ${CMAKE_INSTALL_PREFIX})
 set (exec_prefix "\${prefix}")
 set (libdir "${CMAKE_INSTALL_FULL_LIBDIR}")
@@ -38,12 +39,15 @@ if (NOT MSVC)
 
     find_package (PkgConfig REQUIRED)
 
-    find_package(msgpack-cxx CONFIG)
-    if(msgpack-cxx_FOUND)
-    set(MSGPACK_LIB msgpack-cxx)
-    else()
-    find_package(msgpackc-cxx CONFIG REQUIRED NAMES msgpackc-cxx msgpack)
-    set(MSGPACK_LIB msgpackc-cxx)
+    check_include_file_cxx(msgpack.hpp HAVE_MSGPACKCXX)
+    if (NOT HAVE_MSGPACKCXX)
+        find_package(msgpack QUIET CONFIG NAMES msgpack msgpackc-cxx)
+        if (NOT msgpack_FOUND)
+            find_package(msgpack REQUIRED CONFIG NAMES msgpack-cxx)
+            set(MSGPACK_TARGET "msgpack-cxx")
+        else()
+            set(MSGPACK_TARGET "msgpackc-cxx")
+        endif()
     endif()
 
     find_package(fmt)
@@ -184,6 +188,9 @@ else()
         ${WIN32_DEP_DIR}/../msvc/lib/libfmt.lib
         ${WIN32_DEP_DIR}/../msvc/lib/libmsgpackc-cxx.lib)
 endif()
+if (NOT HAVE_MSGPACKCXX)
+    target_link_libraries(opendht PUBLIC ${MSGPACK_TARGET})
+endif()
 if (APPLE)
     target_link_libraries(dhtnet PRIVATE "-framework CoreFoundation" "-framework Security" "resolv")
 endif()