Hello,
I have written a simple package with only one header file. Currently I’m trying to fetch my library from directory and then add it using find_package
. In addition, I’m unable to install
directory of header file in _deps
directory (to be honest I’m not sure it’s needed or is a good practice). Below is the tree directory of the project and then the code of the `CMakeLists’ file.
├── cmake
│ └── utilsConfig.cmake.in
├── CMakeLists.txt
└── include
├── headers.hpp
└── utilities.hpp
code:
cmake_minimum_required(VERSION 3.25)
project(utils)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
add_library(utils INTERFACE)
add_library(libutils::utils ALIAS utils)
target_include_directories(utils INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
install(TARGETS utils
EXPORT ${PROJECT_NAME}Targets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(DIRECTORY include/
TYPE INCLUDE
)
set(configFile ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake)
set(versionFile ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake)
set(configInstallDestination ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
configure_package_config_file(
${CMAKE_CURRENT_SOURCE_DIR}/cmake/utilsConfig.cmake.in
${configFile}
INSTALL_DESTINATION ${configInstallDestination}
)
write_basic_package_version_file(
"${versionFile}"
COMPATIBILITY SameMajorVersion
)
install(FILES
"${configFile}"
"${versionFile}"
DESTINATION
${configInstallDestination}
)
install(EXPORT ${PROJECT_NAME}Targets
NAMESPACE libutils::
DESTINATION ${configInstallDestination}
)
Here is the directory tree of project in which I’m trying to find_package
:
├── CMakeLists.txt
├── test.cpp
└── thirdparty
└── libutils
and code of CMakeLists
:
cmake_minimum_required(VERSION 3.23)
project(Test)
include(FetchContent)
FetchContent_Declare(
utils
SOURCE_DIR "${CMAKE_SOURCE_DIR}/thirdparty/libutils/"
)
FetchContent_MakeAvailable(libutils)
find_package(utils)
add_executable(Test test.cpp)
target_link_libraries(Test PUBLIC libutils::utils)
I want to mention that I can include
utilities.hpp
in test.cpp
thanks to INCLUDES DESTINATION
command. But for some reason CMake
cannot find package.