Hi,
I looked more precisely at the answers above and I’m still confused.
I have actually several use-cases.
Let’s call “mylib” my own library that I export as mylib::mylib (so far, everything is OK)
I’d like to declare a dependency to a third party library “third” that is detectable through find_package.
- third::third exists
my cmakelist.txt contains:
find_package(third REQUIRED)
target_link_libraries(mylib <whatever> third::third)
list(APPEND DEP third)
configure_file(<Path to MyLibConfig.cmake> MyLibConfig.cmake @ONLY)
my mylibConfig.cmake contains:
include(CMakeFindDependencyMacro)
foreach(_lib_ IN ITEMS @DEP@)
find_dependency(${_lib_} REQUIRED)
endforeach()
# include the generated target file
include("${CMAKE_CURRENT_LIST_DIR}/@TARGET_NAME@Targets.cmake")
everything is fine
- third namespace exists but the exported target has a differente name third::third_other (it happens for instance with Eigen that exports Eigen3::Eigen and not Eigen3::Eigen3)
my cmakelist.txt contains:
find_package(third REQUIRED)
target_link_libraries(mylib <whatever> third::third_other)
list(APPEND DEP third)
configure_file(<Path to MyLibConfig.cmake> MyLibConfig.cmake @ONLY)
my mylibConfig.cmake contains:
include(CMakeFindDependencyMacro)
foreach(_lib_ IN ITEMS @DEP@)
find_dependency(${_lib_} REQUIRED)
endforeach()
# include the generated target file
include("${CMAKE_CURRENT_LIST_DIR}/@TARGET_NAME@Targets.cmake")
It still works but I don’t understand why as the name third_other does not seem to be propagated through my MyLibConfig.cmake
- third namespace exists but there is no target third::xxx (for instance OpenCV)
I tried to wrap it in my cmakelist as mentioned in a previous message but it does not work as the wrapped target is not propagate properly with my mylibConfig.cmake (If I understood you correctly)
should I copy my wrapping from the cmakelist to the mylibConfig.cmake file (in pseudo-code):
include(CMakeFindDependencyMacro)
foreach(_lib_ IN ITEMS @DEP@)
find_dependency(${_lib_} REQUIRED)
IF(${_lib_}_FOUND AND NOT TARGET ${_lib_}::${_lib_})
add_library(${_lib_}::${_lib_} UNKNOWN IMPORTED)
set_target_properties(${_lib_}::${_lib_} PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
IMPORTED_LOCATION "${${_lib_}_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${${_lib_}_INCLUDE_DIRS}"
)
endforeach()
So far I found only two unsatisfying solutions:
first one:
my cmakelist.txt contains:
find_package(third REQUIRED)
target_include_directories(mylib <whatever> third_INCLUDE_DIRS)
target_link_libraries(mylib <whatever> third_LIBRARIES)
list(APPEND DEP third)
configure_file(<Path to MyLibConfig.cmake> MyLibConfig.cmake @ONLY)
my mylibConfig.cmake is unchanged
second one:
I create a Findthird.cmake file that looks for third locations and finishes with:
IF(third_FOUND AND NOT TARGET third::third)
add_library(third::third UNKNOWN IMPORTED)
set_target_properties(third::third PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
IMPORTED_LOCATION "${third_LIBRARIES}"
INTERFACE_INCLUDE_DIRECTORIES "${third_INCLUDE_DIRS}"
)
ENDIF()
but both solution create a dependency to all submodules in “third” (see point 4 below).
- how to declare dependency only to a submodule of “third” for each use-case above? (for instance, using only opencv_superres and not all OpenCV libraries
It’s a long post with many questions in one but I hope it will make the problem clearer.
Regards