diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8bfdc475a7a1de720773c5fcf62b32bacb683385..e61dfcc299566c2a562bb94f7846fb64570387a4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -15,6 +15,8 @@ set (includedir "\${prefix}/include")
 option (OPENDHT_PYTHON "Build Python bindings" OFF)
 option (OPENDHT_TOOLS "Build DHT tools" ON)
 option (OPENDHT_DEBUG "Build with debug flags" OFF)
+option (OPENDHT_STATIC "Build static library" ON)
+option (OPENDHT_SHARED "Build shared library" ON)
 
 set (CMAKE_CXX_FLAGS "-pthread -std=c++11 -Wno-return-type -Wall -Wextra -Wnon-virtual-dtor ${CMAKE_CXX_FLAGS}")
 set (CMAKE_CXX_FLAGS "-DMSGPACK_DISABLE_LEGACY_NIL -DMSGPACK_DISABLE_LEGACY_CONVERT ${CMAKE_CXX_FLAGS}")
@@ -74,25 +76,30 @@ else ()
 	set(CMAKE_BUILD_TYPE Release)
 endif ()
 
-add_library (opendht SHARED
-	${opendht_SOURCES}
-	${opendht_HEADERS}
-)
-set_target_properties (opendht PROPERTIES IMPORT_SUFFIX "_import.lib")
-#set_target_properties (opendht PROPERTIES SOVERSION 1 VERSION 1.0.0)
-
-add_library (opendht-static STATIC
-	${opendht_SOURCES}
-	${opendht_HEADERS}
-)
-set_target_properties (opendht-static PROPERTIES OUTPUT_NAME "opendht")
-
 if (NOT DEFINED CMAKE_INSTALL_LIBDIR)
 	set(CMAKE_INSTALL_LIBDIR lib)
 endif ()
 
-target_link_libraries(opendht-static gnutls nettle)
-target_link_libraries(opendht gnutls nettle)
+if (OPENDHT_STATIC)
+	add_library (opendht-static STATIC
+		${opendht_SOURCES}
+		${opendht_HEADERS}
+	)
+	set_target_properties (opendht-static PROPERTIES OUTPUT_NAME "opendht")
+	target_link_libraries(opendht-static gnutls nettle)
+	install (TARGETS opendht-static DESTINATION ${CMAKE_INSTALL_LIBDIR})
+endif ()
+
+if (OPENDHT_SHARED)
+	add_library (opendht SHARED
+		${opendht_SOURCES}
+		${opendht_HEADERS}
+	)
+	set_target_properties (opendht PROPERTIES IMPORT_SUFFIX "_import.lib")
+	#set_target_properties (opendht PROPERTIES SOVERSION 1 VERSION 1.0.0)
+	target_link_libraries(opendht gnutls nettle)
+	install (TARGETS opendht DESTINATION ${CMAKE_INSTALL_LIBDIR})
+endif ()
 
 if (OPENDHT_TOOLS)
 	add_subdirectory(tools)
@@ -102,6 +109,5 @@ if (OPENDHT_PYTHON)
 	add_subdirectory(python)
 endif ()
 
-install (TARGETS opendht opendht-static DESTINATION ${CMAKE_INSTALL_LIBDIR})
 install (DIRECTORY include DESTINATION ${CMAKE_INSTALL_PREFIX})
 install (FILES ${CMAKE_CURRENT_BINARY_DIR}/opendht.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt
index 2db4ef3670806eef2db931089ea3aace27f1faa3..8cccce266b0f529140d25cb07b50486c124d1c32 100644
--- a/tools/CMakeLists.txt
+++ b/tools/CMakeLists.txt
@@ -3,9 +3,19 @@ add_executable (dhtnode dhtnode.cpp tools_common.h)
 add_executable (dhtscanner dhtscanner.cpp tools_common.h)
 add_executable (dhtchat dhtchat.cpp tools_common.h)
 
-target_link_libraries (dhtnode LINK_PUBLIC opendht gnutls readline)
-target_link_libraries (dhtscanner LINK_PUBLIC opendht gnutls readline)
-target_link_libraries (dhtchat LINK_PUBLIC opendht gnutls readline)
+target_link_libraries (dhtnode LINK_PUBLIC gnutls readline)
+target_link_libraries (dhtscanner LINK_PUBLIC gnutls readline)
+target_link_libraries (dhtchat LINK_PUBLIC gnutls readline)
+
+if (OPENDHT_SHARED)
+	target_link_libraries (dhtnode LINK_PUBLIC opendht)
+	target_link_libraries (dhtscanner LINK_PUBLIC opendht)
+	target_link_libraries (dhtchat LINK_PUBLIC opendht)
+else ()
+	target_link_libraries (dhtnode LINK_PUBLIC opendht-static)
+	target_link_libraries (dhtscanner LINK_PUBLIC opendht-static)
+	target_link_libraries (dhtchat LINK_PUBLIC opendht-static)
+endif ()
 
 if (NOT DEFINED CMAKE_INSTALL_BINDIR)
 	set(CMAKE_INSTALL_BINDIR bin)