diff --git a/CMakeLists.txt b/CMakeLists.txt
index fd227252396260ecf12e94acf9317e303156bf37..0f2fd15fc7b0b6b87a5ef2814cc4b34e93198312 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,36 +19,34 @@ if(${CDI_PTHREAD})
   include(FindThreads)
   find_package(Threads REQUIRED)
   set_target_properties(Threads::Threads PROPERTIES THREADS_PREFER_PTHREAD_FLAG TRUE)
-  list(APPEND cdi_compile_defs HAVE_PTHREAD=1)
-  list(APPEND cdi_linked_libs pthread)
+  target_compile_definitions(cdilib PRIVATE HAVE_PTHREAD=1)
+  target_link_libraries(cdilib pthread)
 endif()
 
 # enable default internal libs
 option(CDI_LIBGRIB "GRIB support [default=ON]" ON)
 if(${CDI_LIBGRIB})
-  list(APPEND cdi_compile_defs
-       HAVE_LIBGRIB=1
-       HAVE_LIBGRIB_API=1)
+  target_compile_definitions(cdilib PRIVATE HAVE_LIBGRIB=1 HAVE_LIBGRIB_API=1)
 endif()
 
 option(CDI_LIBGRIBEX "Use the CGRIBEX library [default=ON]" ON)
 if(${CDI_LIBGRIBEX})
-  list(APPEND cdi_compile_defs LIBCGRIBEX=1)
+  target_compile_definitions(cdilib PRIVATE LIBCGRIBEX=1)
 endif()
 
 option(CDI_EXTRA "Use the extra library [default=ON]" ON)
 if(${CDI_EXTRA})
-  list(APPEND cdi_compile_defs HAVE_LIBEXTRA=1)
+  target_compile_definitions(cdilib PRIVATE HAVE_LIBEXTRA=1)
 endif()
 
 option(CDI_IEG "Use the extra library [default=ON]" ON)
 if(${CDI_IEG})
-  list(APPEND cdi_compile_defs HAVE_LIBIEG=1)
+  target_compile_definitions(cdilib PRIVATE HAVE_LIBIEG=1)
 endif()
 
 option(CDI_SERVICE "Use the extra library [default=ON]" ON)
 if(${CDI_SERVICE})
-  list(APPEND cdi_compile_defs HAVE_LIBSERVICE=1)
+  target_compile_definitions(cdilib PRIVATE HAVE_LIBSERVICE=1)
 endif()
 
 # ecCodes
@@ -56,7 +54,9 @@ option(CDI_ECCODES "Use the eccodes library [default=ON]" ON)
 if(${CDI_ECCODES} OR eccodes_ROOT)
   find_package(eccodes REQUIRED)
   if (${eccodes_FOUND})
-    list(APPEND cdi_linked_libs eccodes)
+    target_compile_definitions(cdilib PRIVATE HAVE_LIBGRIB_API=${eccodes_FOUND})
+    message(VERBOSE "added compile definition HAVE_LIBGRIB_API=${eccodes_FOUND}")
+    target_link_libraries(cdilib eccodes)
   endif ()
 endif()
 
@@ -70,7 +70,7 @@ if(${CDI_NETCDF} OR netCDF_ROOT )
       HAVE_LIBNC_DAP=${netCDF_FOUND}
       HAVE_NETCDF4=${netCDF_FOUND}
     )
-    list(APPEND cdi_linked_libs netCDF::netcdf)
+    target_link_libraries(cdilib netCDF::netcdf)
   endif ()
 endif()
 
@@ -78,7 +78,7 @@ message(VERBOSE "looking for config.h in: ${PROJECT_BINARY_DIR}/src")
 
 #adding subdirectories
 ## lib and general files
-list(APPEND cdi_compile_defs CDI=1 CDI_SIZE_TYPE=size_t PACKAGE_NAME="${PROJECT_NAME}" VERSION="${CMAKE_PROJECT_VERSION}")
+target_compile_definitions(cdilib PRIVATE CDI=1 CDI_SIZE_TYPE=size_t PACKAGE_NAME="${PROJECT_NAME}" VERSION="${CMAKE_PROJECT_VERSION}")
 add_subdirectory(src)
 
 #app
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 250f7a92893c2e9f4e94068e75eaa6908d0824f7..1fc148b5efa1d29996faf4419005c8e74ffd1bc3 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,4 +1,5 @@
-list( APPEND cdi_src_files
+
+target_sources(cdilib PRIVATE
   async_worker.c
   async_worker.h
   basetime.c
@@ -139,69 +140,78 @@ list( APPEND cdi_src_files
   zaxis.c
   zaxis.h
 )
-list( APPEND cdi_pio_files
-  cdipio.h
-  pio.c
-  pio.h
-  pio_cdf_int.c
-  pio_cdf_int.h
-  pio_client.c
-  pio_client.h
-  pio_comm.c
-  pio_comm.h
-  pio_conf.c
-  pio_conf.h
-  pio_dbuffer.c
-  pio_dbuffer.h
-  pio_dist_grid.c
-  pio_dist_grid.h
-  pio_id_set.h
-  pio_idxlist_cache.c
-  pio_idxlist_cache.h
-  pio_impl.h
-  pio_interface.c
-  pio_interface.h
-  pio_mpi_fw_at_all.c
-  pio_mpi_fw_at_reblock.c
-  pio_mpi_fw_ordered.c
-  pio_mpinonb.c
-  pio_posixasynch.c
-  pio_posixfpguardsendrecv.c
-  pio_posixnonb.c
-  pio_record_send.c
-  pio_roles.c
-  pio_rpc.c
-  pio_rpc.h
-  pio_serialize.c
-  pio_serialize.h
-  pio_server.c
-  pio_server.h
-  pio_util.c
-  pio_util.h
-  pio_xmap_cache.c
-  pio_xmap_cache.h
-)
 
-list( APPEND cdi_unknown
-  cfortran.h
-  getline.c
-  make_fint.c
-  resource_unpack.c
-  resource_unpack.h
-)
+option(CDI_BUILD_PIO "Build unknown sources in libcdi" OFF)
+mark_as_advanced(CDI_BUILD_PIO)
+if (CDI_BUILD_PIO)
+  target_sources(cdilib PRIVATE
+    cdipio.h
+    pio.c
+    pio.h
+    pio_cdf_int.c
+    pio_cdf_int.h
+    pio_client.c
+    pio_client.h
+    pio_comm.c
+    pio_comm.h
+    pio_conf.c
+    pio_conf.h
+    pio_dbuffer.c
+    pio_dbuffer.h
+    pio_dist_grid.c
+    pio_dist_grid.h
+    pio_id_set.h
+    pio_idxlist_cache.c
+    pio_idxlist_cache.h
+    pio_impl.h
+    pio_interface.c
+    pio_interface.h
+    pio_mpi_fw_at_all.c
+    pio_mpi_fw_at_reblock.c
+    pio_mpi_fw_ordered.c
+    pio_mpinonb.c
+    pio_posixasynch.c
+    pio_posixfpguardsendrecv.c
+    pio_posixnonb.c
+    pio_record_send.c
+    pio_roles.c
+    pio_rpc.c
+    pio_rpc.h
+    pio_serialize.c
+    pio_serialize.h
+    pio_server.c
+    pio_server.h
+    pio_util.c
+    pio_util.h
+    pio_xmap_cache.c
+    pio_xmap_cache.h
+  )
+endif()
 
 option(CDI_BUILD_UNKNOWN "Build unknown sources in libcdi" ON)
 mark_as_advanced(CDI_BUILD_UNKNOWN)
 if (CDI_BUILD_UNKNOWN)
-  list(APPEND cdi_src_files ${cdi_unknown})
+  target_sources( cdilib PRIVATE
+    cfortran.h
+    getline.c
+    make_fint.c
+    resource_unpack.c
+    resource_unpack.h
+  )
 endif ()
 
-# Support exporting all symbolds on Windows
+# Support exporting all symbols on Windows
 set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
 
+set_property(TARGET cdilib PROPERTY CDI_INSTALL_HEADERS
+  ${CMAKE_CURRENT_SOURCE_DIR}/cdi.h
+  ${CMAKE_CURRENT_SOURCE_DIR}/calendar.h
+  ${CMAKE_CURRENT_SOURCE_DIR}/cdi_datetime.h
+  ${CMAKE_CURRENT_SOURCE_DIR}/julian_date.h
 )
 
 target_include_directories(cdilib PRIVATE "${PROJECT_BINARY_DIR}/src/config.h")
+
 target_link_libraries(cdilib ${cdi_linked_libs})
 target_compile_definitions(cdilib PRIVATE PACKAGE_NAME="${PROJECT_NAME}" VERSION="${CMAKE_PROJECT_VERSION}" ${cdi_compile_defs})