CMake packages¶
This modules allow to work with the target-based approach of CMake introduced in CMake 2.8.12.
Generating a CMake package¶
You should first add the line set(PROJECT_USE_CMAKE_EXPORT TRUE)
in the header of your project.
Compared to the default minimal project one needs to EXPORT ${TARGETS_EXPORT_NAME}
for at least one target the project should export.
Note that all exported targets will be in the ${PROJECT_NAME}
namespace.
Extra information¶
In some situations you might want to add some extra information to the
generated config script. This can be done by manipulating the
PROJECT_EXTRA_MACROS
variable. The content of this variable will be
appended to the end of the config script.
Consuming packages¶
Minimal working example with CMake packages¶
# Target-based approach should work from CMake 2.8.12 but it should fully work
# from 3.1
cmake_minimum_required(VERSION 2.8.12)
# These variables have to be defined before running SETUP_PROJECT
set(PROJECT_NAME jrl-cmakemodules-minimal-working-example)
set(PROJECT_DESCRIPTION "A project description")
set(PROJECT_URL http://jrl-cmakemodules.readthedocs.io)
set(PROJECT_USE_CMAKE_EXPORT TRUE)
include(cmake/base.cmake)
project(${PROJECT_NAME} CXX)
# Add a required dependency
add_project_dependency(MyDependency REQUIRED)
# Another example to show that arguments can be passed down to the underlying
# find_package call
add_project_dependency(Boost 1.50 REQUIRED COMPONENT timer)
add_library(myLibrary ${MY_SOURCES})
target_link_libraries(myLibrary MyDependency::MyAwesomeLib Boost::timer)
install(
TARGETS myLibrary
EXPORT ${TARGETS_EXPORT_NAME}
DESTINATION lib)