diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9f60838652898f39c490176d0ce461ce41b85567..f65816728ae0bcdb4590a44b07663d598e8fd262 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2,9 +2,11 @@
 #
 #  @copyright 2017-2019 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
 #                       Univ. Bordeaux. All rights reserved.
+#  @copyright 2020 Inria. All rights reserved.
 #
 #  @version 0.1.0
 #  @author Mathieu Faverge
+#  @author Florent Pruvost
 #  @date 2017-04-26
 #
 ###
@@ -12,7 +14,7 @@ cmake_minimum_required (VERSION 3.0)
 project (HQR C)
 
 # Check if compiled independently or within another project
-if ( ${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
+if ( ${HQR_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
   set( BUILD_SUBPROJECT OFF )
 else()
   set( BUILD_SUBPROJECT ON )
@@ -59,10 +61,6 @@ int main(void) {
   HAVE_FALLTHROUGH
   FAIL_REGEX ".*fallthrough.*" )
 
-if ( HAVE_FALLTHROUGH )
-  add_definitions( -DLIBHQR_HAVE_FALLTHROUGH )
-endif()
-
 ### Misc options
 if ( NOT BUILD_SUBPROJECT )
   option(BUILD_SHARED_LIBS
@@ -72,44 +70,8 @@ if ( NOT BUILD_SUBPROJECT )
   endif(NOT CMAKE_BUILD_TYPE)
 endif()
 
-set(hdrs
-  include/libhqr.h
-  include/libhqr_internal.h
-  include/libhqr_queue.h
-)
-set(srcs
-  # Low level tree functions
-  src/low_flat.c
-  src/low_binary.c
-  src/low_fibonacci.c
-  src/low_greedy.c
-  src/low_greedy1p.c
-  src/low_adaptiv.c
-  # High level tree functions
-  src/high_flat.c
-  src/high_binary.c
-  src/high_fibonacci.c
-  src/high_greedy.c
-  # Direct access tree functions
-  src/systolic.c
-  src/svd.c
-  src/hqr.c
-  src/tphqr.c
-  src/tshqr.c
-  src/mtxtree.c
-  # Others
-  src/check.c
-  src/gendot.c
-  src/gensvg.c
-  src/print.c
-  src/queue.c
-)
-
-include_directories(include)
-add_library(hqr ${srcs})
-set_property(TARGET hqr PROPERTY INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/lib")
-
-target_link_libraries( hqr m )
+# The compiled library code is here
+add_subdirectory(src)
 
 ###############################################################################
 # Executables and Tests #
@@ -119,24 +81,6 @@ include(CTest)
 
 add_subdirectory(testings)
 
-###############################################################################
-# Installation
-#
-install(FILES
-  include/libhqr.h
-  DESTINATION include )
-
-install(TARGETS hqr
-  RUNTIME DESTINATION bin
-  ARCHIVE DESTINATION lib
-  LIBRARY DESTINATION lib)
-
-###############################################################################
-# Config files (pkg_config, hqr_env.sh)
-#
-generate_hqr_pkgconfig_file()
-generate_env_file()
-
 #-- Add a custom target to generate tags
 add_custom_target (tags
   COMMAND "git ls-files | xargs etags" )
diff --git a/cmake_modules/GenHQRPkgConfig.cmake b/cmake_modules/GenHQRPkgConfig.cmake
index e9a4950e93f9bb294f078534368598fe6de9e0f9..54b7749a5293b078c3a8128180dc894457b73ae2 100644
--- a/cmake_modules/GenHQRPkgConfig.cmake
+++ b/cmake_modules/GenHQRPkgConfig.cmake
@@ -1,14 +1,11 @@
 ###
 #
-# @copyright (c) 2009-2014 The University of Tennessee and The University
-#                          of Tennessee Research Foundation.
-#                          All rights reserved.
 # @copyright (c) 2012-2017 Inria. All rights reserved.
 # @copyright (c) 2012-2014 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria, Univ. Bordeaux. All rights reserved.
 #
 ###
 #
-#  @file GenPkgConfig.cmake
+#  @file GenHQRPkgConfig.cmake
 #
 #  @project MORSE
 #  MORSE is a software package provided by:
@@ -18,61 +15,13 @@
 #     Univ. of California Berkeley,
 #     Univ. of Colorado Denver.
 #
-#  @version 0.9.1
-#  @author Cedric Castagnede
-#  @author Emmanuel Agullo
+#  @version 0.1.0
 #  @author Mathieu Faverge
 #  @author Florent Pruvost
 #  @date 10-11-2014
 #
 ###
 
-###
-#
-# CONVERT_LIBSTYLE_TO_PKGCONFIG: convert a libraries list to follow the pkg-config style
-#                                used in CLEAN_LIB_LIST
-#
-###
-# macro(CONVERT_LIBSTYLE_TO_PKGCONFIG _liblist)
-#     set(${_liblist}_CPY "${${_liblist}}")
-#     set(${_liblist} "")
-#     foreach(_dep ${${_liblist}_CPY})
-#         if (${_dep} MATCHES "^/")
-#             get_filename_component(dep_libname ${_dep} NAME)
-#             get_filename_component(dep_libdir  ${_dep} DIRECTORY)
-#             string(REPLACE "lib"    "" dep_libname "${dep_libname}")
-#             string(REPLACE ".so"    "" dep_libname "${dep_libname}")
-#             string(REPLACE ".a"     "" dep_libname "${dep_libname}")
-#             string(REPLACE ".dylib" "" dep_libname "${dep_libname}")
-#             string(REPLACE ".dll"   "" dep_libname "${dep_libname}")
-#             list(APPEND ${_liblist} -L${dep_libdir} -l${dep_libname})
-#         elseif(NOT ${_dep} MATCHES "^-")
-#             list(APPEND ${_liblist} "-l${_dep}")
-#         else()
-#             list(APPEND ${_liblist} ${_dep})
-#         endif()
-#     endforeach()
-# endmacro(CONVERT_LIBSTYLE_TO_PKGCONFIG)
-
-###
-#
-# CLEAN_LIB_LIST: clean libraries lists to follow the pkg-config style
-#                 used in GENERATE_PKGCONFIG_FILE
-#
-###
-#macro(CLEAN_LIB_LIST _package)
-#    list(REMOVE_DUPLICATES ${_package}_PKGCONFIG_LIBS)
-#    list(REMOVE_DUPLICATES ${_package}_PKGCONFIG_LIBS_PRIVATE)
-#    list(REMOVE_DUPLICATES ${_package}_PKGCONFIG_REQUIRED)
-#    list(REMOVE_DUPLICATES ${_package}_PKGCONFIG_REQUIRED_PRIVATE)
-#    convert_libstyle_to_pkgconfig(${_package}_PKGCONFIG_LIBS)
-#    convert_libstyle_to_pkgconfig(${_package}_PKGCONFIG_LIBS_PRIVATE)
-#    string(REPLACE ";" " " ${_package}_PKGCONFIG_LIBS "${${_package}_PKGCONFIG_LIBS}")
-#    string(REPLACE ";" " " ${_package}_PKGCONFIG_LIBS_PRIVATE "${${_package}_PKGCONFIG_LIBS_PRIVATE}")
-#    string(REPLACE ";" " " ${_package}_PKGCONFIG_REQUIRED "${${_package}_PKGCONFIG_REQUIRED}")
-#    string(REPLACE ";" " " ${_package}_PKGCONFIG_REQUIRED_PRIVATE "${${_package}_PKGCONFIG_REQUIRED_PRIVATE}")
-#endmacro(CLEAN_LIB_LIST)
-
 ###
 #
 # GENERATE_PKGCONFIG_FILE: generate files hqr.pc
@@ -89,7 +38,7 @@ macro(GENERATE_HQR_PKGCONFIG_FILE)
 
   set(_output_hqr_file "${CMAKE_BINARY_DIR}/hqr.pc")
   configure_file(
-    "${CMAKE_CURRENT_SOURCE_DIR}/lib/pkgconfig/hqr.pc.in"
+    "${HQR_SOURCE_DIR}/lib/pkgconfig/hqr.pc.in"
     "${_output_hqr_file}"
     @ONLY
     )
@@ -110,7 +59,7 @@ macro(generate_env_file)
     # Create .sh file
     # ---------------
     configure_file(
-      "${CMAKE_CURRENT_SOURCE_DIR}/hqr_env.sh.in"
+      "${HQR_SOURCE_DIR}/hqr_env.sh.in"
       "${CMAKE_BINARY_DIR}/bin/hqr_env.sh" @ONLY)
 
     # installation
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000000000000000000000000000000000000..baeec1c4d4a490be9f750025589647d00122b5e3
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,105 @@
+###
+#
+#  @copyright 2017-2019 Bordeaux INP, CNRS (LaBRI UMR 5800), Inria,
+#                       Univ. Bordeaux. All rights reserved.
+#  @copyright 2020 Inria. All rights reserved.
+#
+#  @version 0.1.0
+#  @author Mathieu Faverge
+#  @author Florent Pruvost
+#  @date 2020-12-14
+#
+###
+
+# Note that headers are optional, and do not affect add_library, but they will not
+# show up in IDEs unless they are listed in add_library.
+set(HEADER_LIST
+  ${HQR_SOURCE_DIR}/include/libhqr.h
+  ${HQR_SOURCE_DIR}/include/libhqr_internal.h
+  ${HQR_SOURCE_DIR}/include/libhqr_queue.h
+)
+set(SOURCE_LIST
+  # Low level tree functions
+  low_flat.c
+  low_binary.c
+  low_fibonacci.c
+  low_greedy.c
+  low_greedy1p.c
+  low_adaptiv.c
+  # High level tree functions
+  high_flat.c
+  high_binary.c
+  high_fibonacci.c
+  high_greedy.c
+  # Direct access tree functions
+  systolic.c
+  svd.c
+  hqr.c
+  tphqr.c
+  tshqr.c
+  mtxtree.c
+  # Others
+  check.c
+  gendot.c
+  gensvg.c
+  print.c
+  queue.c
+)
+
+# Make an automatic library - will be static or dynamic based on user setting
+add_library(hqr ${SOURCE_LIST} ${HEADER_LIST})
+
+set_target_properties(hqr PROPERTIES VERSION ${HQR_VERSION})
+
+# We need this directory, to build.
+target_include_directories(hqr PUBLIC
+  $<BUILD_INTERFACE:${HQR_SOURCE_DIR}/include>
+  $<INSTALL_INTERFACE:include>)
+
+# Users of our library will need the header defining the API
+set_target_properties(hqr PROPERTIES PUBLIC_HEADER ${HQR_SOURCE_DIR}/include/libhqr.h)
+
+# Depends on m at link
+target_link_libraries(hqr PRIVATE m)
+
+# Add definitions
+if ( HAVE_FALLTHROUGH )
+  target_compile_definitions(hqr PRIVATE LIBHQR_HAVE_FALLTHROUGH)
+endif()
+
+# Target export
+install(EXPORT hqrTargets
+        FILE HQRTargets.cmake
+        NAMESPACE HQR::
+        DESTINATION lib/cmake/hqr)
+
+# Install executables, headers, lib, targets file
+install(TARGETS hqr
+        EXPORT hqrTargets
+        RUNTIME DESTINATION bin
+        PUBLIC_HEADER DESTINATION include
+        ARCHIVE DESTINATION lib
+        LIBRARY DESTINATION lib)
+
+# see https://cmake.org/cmake/help/latest/module/CMakePackageConfigHelpers.html
+include(CMakePackageConfigHelpers)
+
+set(BIN_INSTALL_DIR "bin/" CACHE STRING "where to install executables relative to prefix" )
+set(INC_INSTALL_DIR "include/" CACHE STRING "where to install headers relative to prefix" )
+set(LIB_INSTALL_DIR "lib/" CACHE STRING "where to install libraries relative to prefix" )
+
+configure_package_config_file(HQRConfig.cmake.in
+                              ${CMAKE_CURRENT_BINARY_DIR}/HQRConfig.cmake
+                              INSTALL_DESTINATION ${LIB_INSTALL_DIR}/cmake/hqr
+                              PATH_VARS BIN_INSTALL_DIR INC_INSTALL_DIR LIB_INSTALL_DIR)
+write_basic_package_version_file(HQRConfigVersion.cmake
+                                 VERSION ${HQR_VERSION}
+                                 COMPATIBILITY AnyNewerVersion)
+
+# Install config files
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/HQRConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/HQRConfigVersion.cmake
+        DESTINATION ${LIB_INSTALL_DIR}/cmake/hqr)
+
+# Config files (pkg_config, hqr_env.sh)
+generate_hqr_pkgconfig_file()
+generate_env_file()
diff --git a/src/HQRConfig.cmake.in b/src/HQRConfig.cmake.in
new file mode 100644
index 0000000000000000000000000000000000000000..936290480bfa25ed501e2a60ab125125b0015409
--- /dev/null
+++ b/src/HQRConfig.cmake.in
@@ -0,0 +1,20 @@
+set(HQR_VERSION @HQR_VERSION@)
+
+@PACKAGE_INIT@
+
+set_and_check(HQR_BIN_DIR "@PACKAGE_BIN_INSTALL_DIR@")
+set_and_check(HQR_INC_DIR "@PACKAGE_INC_INSTALL_DIR@")
+set_and_check(HQR_LIB_DIR "@PACKAGE_LIB_INSTALL_DIR@")
+
+check_required_components(HQR)
+
+
+# This commented material could be useful later
+#include(CMakeFindDependencyMacro)
+# Same syntax as find_package
+#find_dependency(MYDEP REQUIRED)
+
+# Any extra setup
+
+# Add the targets file
+include("${CMAKE_CURRENT_LIST_DIR}/HQRTargets.cmake")
\ No newline at end of file
diff --git a/testings/CMakeLists.txt b/testings/CMakeLists.txt
index 012a2a1cce5de7f65ce1e4abdb8d69e1756e6105..fbef029c209c88bd737025f00c95f26f77980a7a 100644
--- a/testings/CMakeLists.txt
+++ b/testings/CMakeLists.txt
@@ -9,8 +9,6 @@
 #
 ###
 
-include_directories(${CMAKE_CURRENT_SOURCE_DIR})
-
 if ( BUILD_SUBPROJECT )
   set( HQR_INSTALL_DIR bin/hqr )
 else()
@@ -32,7 +30,7 @@ foreach (_file ${TESTINGS})
   get_filename_component(_name_we ${_file} NAME_WE)
   add_executable(${_name_we}
     ${_file} common.c)
-  target_link_libraries(${_name_we} hqr)
+  target_link_libraries(${_name_we} PRIVATE hqr)
 
   install(
     TARGETS ${_name_we}