Create CMake / CPack <Library> Config.cmake for the shared library

I have the simplest possible c-library that builds and packs using the following CMakeLists.txt :

 cmake_minimum_required(VERSION 3.5) project (libfoo C) add_library(foo SHARED impl.c) target_link_libraries(foo) install(TARGETS foo LIBRARY DESTINATION lib/) install(FILES public_header.h DESTINATION include/libfoo) set(CPACK_GENERATOR "TGZ") include(CPack) 

A working example is here: https://github.com/bjarkef/cmake-simple/tree/master/libfoo

I am running mkdir -p build; (cd build/; cmake ../; make all package;) mkdir -p build; (cd build/; cmake ../; make all package;) to build a .tar.gz package with a compiled shared library along with its public header file. It all works fine.

Now I want to modify CMakeLists.txt to create the FooConfig.cmake and FooConfigVersion.cmake files needed for CMake find_package in another project to find the foo library. How to do it?

I found that I have to use CMakePackageConfigHelpers : configure_package_config_file and write_basic_package_version_file , and I have to create the file FooLibraryConfig.cmake.in , however, I cannot figure out how to put it all together.

Note that it is important that the resulting .cmake files contain only relative paths.

+5
source share
1 answer

I have a cmake module included in the top level of CmakeList.txt:

 # Generate and install package config files include(PackageConfigInstall) 

In the general PackageConfigInstall.cmake file, configuration files are created from cmake.in files and installed. This module can be reused for other packages.

 include(CMakePackageConfigHelpers) # Generate package config cmake files set(${PACKAGE_NAME}_LIBRARY_NAME ${CMAKE_SHARED_LIBRARY_PREFIX}${PACKAGE_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}) configure_package_config_file(${PACKAGE_NAME}-config.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake INSTALL_DESTINATION ${CMAKE_INSTALL_DIR}/${PACKAGE_NAME} PATH_VARS LIB_INSTALL_DIR INCLUDE_INSTALL_DIR APP_INCLUDE_INSTALL_DIR ) configure_file(${PACKAGE_NAME}-config-version.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake @ONLY) # Install package config cmake files install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}-config-version.cmake DESTINATION ${CMAKE_INSTALL_DIR}/${PACKAGE_NAME} COMPONENT devel ) 

You will need a batch file for your library, such as your_lib-config.cmake.in, which will become your_lib-config.cmake. This will contain the include and library variables that can be used.

 get_filename_component(YOUR_LIB_CMAKE_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH) # flag required by CMakePackageConfigHelpers @ [email protected] set_and_check(YOUR_LIB_INCLUDE_DIR @ [email protected] /hal) set_and_check(YOUR_LIB_LIBRARY @ [email protected] /@ [email protected] @ [email protected] @ [email protected] ) set_and_check(YOUR_LIB_LIBRARIES @ [email protected] /@ [email protected] @ [email protected] @ [email protected] ) 

You will also need the config-version.cmake.in file as follows:

 set(PACKAGE_VERSION @ [email protected] ) # Check whether the requested PACKAGE_FIND_VERSION is compatible if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}") set(PACKAGE_VERSION_COMPATIBLE FALSE) else() set(PACKAGE_VERSION_COMPATIBLE TRUE) if ("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}") set(PACKAGE_VERSION_EXACT TRUE) endif() endif() 

In the packaging scenarios, itโ€™s not enough for everything to be in order. I went through a lot of trial and error to finally get something that works for different purposes (both the linux server and the built-in target). I could leave something, so please just comment and I will update the answer.

0
source

All Articles