I'm trying to set up the build process of my library using cmake's install/export, for which the built lib and header files will be packaged up as a .deb using dpkg-buildpackage
. I was trying to follow the guide on https://cmake.org/cmake/help/git-stage/guide/importing-exporting/index.html for the most part, so as to make my package findable after install using find_package
.
The issue I'm facing is when my libraries have nested dependencies from my other folders/projects, which I add using add_subdirectory()
.
My main CMake for the project I want to build/export is
cmake_minimum_required(VERSION 3.10)
project(exampleLib)
add_subdirectory(../path/to/dependencyA ${PROJECT_BINARY_DIR}/dependencyA)
include(GNUInstallDirs)
add_library(exampleLib SHARED
exampleLib.cpp
)
target_link_libraries(exampleLib PRIVATE
dependencyA
)
target_include_directories(dashStreamerClient PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
# dependencyA has a dependency on "dependencyB"
# which if I don't include here, errors with 'CMake Error: install(EXPORT "exampleLibTargets" ...) includes target "dependencyA" which requires target "dependencyB" that is not in any export set.'
install(TARGETS exampleLib dependencyA dependencyB
EXPORT exampleLibTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
# Note: I only want this exampleLib.h file to be "installed" in CMAKE_INSTALL_INCLUDEDIR, not any of the other dependencies headers as they are unnecessary!
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/exampleLib.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(EXPORT exampleLibTargets
FILE exampleLibTargets.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/exampleLib
)
# Make a package configuration file
include(CMakePackageConfigHelpers)
# Config.cmake.in copied from the importing-exporting cmake guide
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/exampleLibConfig.cmake"
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/exampleLib
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/exampleLibConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/exampleLibConfigVersion.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/exampleLib
)
export(EXPORT exampleLibTargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/exampleLibTargets.cmake"
)
while my dependencyA's CMakeLists.txt is
cmake_minimum_required(VERSION 3.10)
project(dependencyA)
add_subdirectory(path/to/dependencyB ${PROJECT_BINARY_DIR}/dependencyB)
add_library(dependencyA SHARED
dependencyA.cpp
)
target_link_libraries(dependencyA PUBLIC
dependencyB
)
target_include_directories(dependencyA PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
)
The issue is when I try to make install
, I get the error
CMake Error at cmake_install.cmake:152 (file):
file INSTALL cannot find
"/home/me/path/to/exampleLib/include/dependencyB.h":
No such file or directory.
where CMake tries to find the header of the nested dependencyB inside my current CMake project's path, but dependencyB.h
is actually in e.g. path/to/dependencyB
which was defined in the add_subdirectory of dependencyA. I also do not actually want this dependencyB.h
to be installed, as it is not necessary, and only want exampleLib.h
.
What am I doing wrong?
Thank you!