diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5054cd361d0980bb963bdf173831a02649b9eded..610bdc256bc077d05627f2dcbbf2d30fde516f07 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,6 +17,7 @@ if(${CDI_PTHREAD})
   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)
 endif()
 
 # enable default internal libs
@@ -53,6 +54,7 @@ if(${CDI_ECCODES} OR eccodes_ROOT)
   if (${eccodes_FOUND})
     list(APPEND cdi_compile_defs HAVE_LIBGRIB_API=${eccodes_FOUND})
     message(VERBOSE "added compile definition HAVE_LIBGRIB_API=${eccodes_FOUND}")
+    list(APPEND cdi_linked_libs eccodes)
   else()
     message(WARNING "eccodes not found, compiling without eccodes")
   endif ()
@@ -70,6 +72,7 @@ if(${CDI_NETCDF} OR netCDF_ROOT )
       HAVE_LIBGRIB_API=${netCDF_FOUND}
       HAVE_LIBGRIB=${netCDF_FOUND}
     )
+    list(APPEND cdi_linked_libs netCDF::netcdf)
   else()
     message(WARNING "netcdf target not found, compiling without netcdf")
   endif ()
diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt
index ff3047c998e1452196ce33d84efc7db3088ef98b..4264972e74952dfc37e3368f88c30b0ced8ecc6e 100644
--- a/app/CMakeLists.txt
+++ b/app/CMakeLists.txt
@@ -7,6 +7,6 @@ target_include_directories(cdi PRIVATE
   $<INSTALL_INTERFACE:include
 )
 
-target_link_libraries(cdi PRIVATE cdilib netCDF::netcdf pthread)
+target_link_libraries(cdi PRIVATE cdilib ${cdi_linked_libs})
 target_include_directories(cdi PRIVATE "${PROJECT_BINARY_DIR}/src/config.h")
 target_compile_definitions(cdi PRIVATE  ${cdi_netcdf_definitions})
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 8328f34e5016f9d80817a13c45f0a907e2c33801..f21e7c0235ba86a5a168df6c1b69303adb7c4da9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -199,7 +199,7 @@ add_library(cdilib
 )
 
 target_include_directories(cdilib PRIVATE "${PROJECT_BINARY_DIR}/src/config.h")
-target_link_libraries(cdilib eccodes netCDF::netcdf)
+target_link_libraries(cdilib ${cdi_linked_libs})
 target_compile_definitions(cdilib PRIVATE PACKAGE_NAME="${PROJECT_NAME}" VERSION="${CMAKE_PROJECT_VERSION}" ${cdi_compile_defs})
 
 add_library(cdilib::cdilib ALIAS cdilib)
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index cf18bbac6f3fe2a57236f2f68722ee26ea97c9a2..2e02cf49b4dee098c5d3cc809d18159f47944de3 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -31,11 +31,11 @@ list(APPEND test_lib_src
 
 add_library(test_lib ${test_lib_src})
 target_include_directories(test_lib PRIVATE ../src/)
-target_link_libraries(test_lib PRIVATE cdilib eccodes)
+target_link_libraries(test_lib PRIVATE cdilib ${cdi_linked_libs})
 
 foreach(target ${current_targets})
   target_include_directories(${target} PRIVATE ../src/)
-  target_link_libraries(${target} PRIVATE cdilib test_lib ${netCDF_LIBRARIES} )
+  target_link_libraries(${target} PRIVATE cdilib test_lib ${cdi_linked_libs})
   add_test(NAME ${target} COMMAND ${target})
   if(labels_${target})
     set_property(TEST ${target} PROPERTY LABELS labels_${target})