CMake: config mode of find_package command (examples)

Overview

Build Status Build status

Install Foo

Install project Foo in Debug and Release variants (Makefile generator):

> cmake -HFoo -B_builds/Foo-debug -DCMAKE_BUILD_TYPE=Debug -DCMAKE_DEBUG_POSTFIX=d -DCMAKE_INSTALL_PREFIX="`pwd`/_install"
> cmake --build _builds/Foo-debug --target install
...
Install the project...
-- Install configuration: "Debug"
-- Installing: /.../_install/lib/libbard.a
-- Installing: /.../_install/lib/libbazd.a
-- Installing: /.../_install/include/foo
-- Installing: /.../_install/include/foo/Bar.hpp
-- Installing: /.../_install/include/foo/Baz.hpp
-- Installing: /.../_install/include/foo/BAR_EXPORT.h
-- Installing: /.../_install/include/foo/BAZ_EXPORT.h
-- Installing: /.../_install/lib/cmake/Foo/FooConfig.cmake
-- Installing: /.../_install/lib/cmake/Foo/FooConfigVersion.cmake
-- Installing: /.../_install/lib/cmake/Foo/FooTargets.cmake
-- Installing: /.../_install/lib/cmake/Foo/FooTargets-debug.cmake
> cmake -HFoo -B_builds/Foo-release -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="`pwd`/_install"
> cmake --build _builds/Foo-release --target install
...
Install the project...
-- Install configuration: "Release"
-- Installing: /.../_install/lib/libbar.a
-- Installing: /.../_install/lib/libbaz.a
-- Up-to-date: /.../_install/include/foo
-- Up-to-date: /.../_install/include/foo/Bar.hpp
-- Up-to-date: /.../_install/include/foo/Baz.hpp
-- Installing: /.../_install/include/foo/BAR_EXPORT.h
-- Installing: /.../_install/include/foo/BAZ_EXPORT.h
-- Installing: /.../_install/lib/cmake/Foo/FooConfig.cmake
-- Installing: /.../_install/lib/cmake/Foo/FooConfigVersion.cmake
-- Installing: /.../_install/lib/cmake/Foo/FooTargets.cmake
-- Installing: /.../_install/lib/cmake/Foo/FooTargets-release.cmake

Note that:

  • library target bar for different build types has different names: libbar.a and libbard.a
  • header files is equal for both variants
  • cmake-config files FooConfig.cmake, FooConfigVersion.cmake and FooTargets.cmake is equal for both variants
  • FooTargets-release.cmake set Release imported target properties, e.g. IMPORTED_LOCATION_RELEASE
  • FooTargets-debug.cmake set Debug imported target properties, e.g. IMPORTED_LOCATION_DEBUG

Note:

Boo (use installed Foo)

Easiest way to find and include FooConfig.cmake file is to set CMAKE_INSTALL_PREFIX:

> cmake -HBoo -B_builds/Boo -DCMAKE_INSTALL_PREFIX="`pwd`/_install"

Also CMAKE_PREFIX_PATH and Foo_DIR can be used (do not forget to remove _builds/Boo directory before every configure):

> cmake -HBoo -B_builds/Boo -DCMAKE_PREFIX_PATH="`pwd`/_install"
> cmake -HBoo -B_builds/Boo -DFoo_DIR="`pwd`/_install/lib/cmake/Foo"

find_package config-mode command will include FooConfig.cmake file and import new target Foo::bar:

> cat Boo/CMakeLists.txt 
find_package(Foo CONFIG REQUIRED)
add_executable(boo boo.cpp)
target_link_libraries(boo Foo::bar)

Note that:

  • definition FOO_BAR_DEBUG will be added automatically
  • include directory for target Foo::bar will be added automatically
  • in Debug-mode macro FOO_BAR_DEBUG will be 1 and linker will use libbard.a library
  • in Release-mode macro FOO_BAR_DEBUG will be 0 and linker will use libbar.a library
  • if find_package command specify library version then FooConfigVersion.cmake module will check compatibility:
> grep find_package Boo/CMakeLists.txt 
find_package(Foo 2.0 CONFIG REQUIRED)
> cmake -HBoo -B_builds/Boo -DCMAKE_INSTALL_PREFIX="`pwd`/_install"
CMake Error at CMakeLists.txt:8 (find_package):
  Could not find a configuration file for package "Foo" that is compatible
  with requested version "2.0".

The following configuration files were considered but not accepted:

    /.../_install/lib/cmake/Foo/FooConfig.cmake, version: 1.2.3

Script

See jenkins.py script for automatic testing + options --install-boo/--shared and --monolithic.

UML sequence diagram

uml

More

Issues
  • Issue Finding Package

    Issue Finding Package

    I gave your example a try and I like how it is structured as well as how clear and concise the CMake is, but I ran into a problem. I ran install and successfully "installed" the Foo project.

    When I try to use Foo::bar and Foo::baz in another project, the find_package fails because the check_components function fails when it's searching for the required components. Any ideas how to fix this so that the components are successfully found? If that check isn't there everything works fine.

    opened by DeveloperPaul123 11
  • Monolithic build works only if commands add_subdirectory are in the right order

    Monolithic build works only if commands add_subdirectory are in the right order

    Thank you for this example. There is something that does not seem satisfying: the monolithic build works only if the order of commands add_subdirectory in the top-level CMakeLists.txt is correct. Is there not a way to improve on this? (Imagine a set of a dozen libraries with various dependencies between them.)

    question 
    opened by lguez 6
  • Automating install of third parties packages

    Automating install of third parties packages

    I would like to setup third parties project like proposed in this example.

    However, I would like that the setup of these third parties package are done by my main project cmake.

    If the third party package is not there, then install it .... (most of them are git repository based)

    have you got an idea on how to proceed ?

    opened by sancelot 3
  • Use `LIB_INSTALL_DIR`

    Use `LIB_INSTALL_DIR`

    @mrostecki added this pull request to my project. I asked him to please post it here but I don't think he has had time to do so. I was wondering how you feel about this issue. Below is a quote from the linked PR.

    The common practice in cmake to allow to specify the libdir is using the LIB_INSTALL_DIR variable. The main use case of that variable is setting /usr/lib64 as the destination dir for libraries which is a common pattern in many Linux distributions.

    opened by isaachier 2
  • [CMakeLists.txt] Quoting and using name from environment

    [CMakeLists.txt] Quoting and using name from environment

    [.gitignore] Add common ignore formats

    PS: Is it just me or is CMake far more complicated and convoluted than is needed? - Maybe I've just been spoiled in the Rust world with Cargo…

    opened by SamuelMarks 0
A toolchain file and examples using cmake for iOS development

ios-cmake A toolchain file and examples using cmake for iOS development. This is a fork of a similar project found on https://code.google.com/p/ios-cm

Bogdan Cristea 302 Jun 26, 2022
📦 CMake's missing package manager. A small CMake script for setup-free, cross-platform, reproducible dependency management.

Setup-free CMake dependency management CPM.cmake is a CMake script that adds dependency management capabilities to CMake. It's built as a thin wrapper

CPM.cmake 1.2k Jun 27, 2022
CMake checks cache helper modules – for fast CI CMake builds!

cmake-checks-cache Cross platform CMake projects do platform introspection by the means of "Check" macros. Have a look at CMake's How To Write Platfor

Cristian Adam 58 May 22, 2022
CMake scripts for painless usage of SuiteSparse+METIS from Visual Studio and the rest of Windows/Linux/OSX IDEs supported by CMake

CMake scripts for painless usage of Tim Davis' SuiteSparse (CHOLMOD,UMFPACK,AMD,LDL,SPQR,...) and METIS from Visual Studio and the rest of Windows/Lin

Jose Luis Blanco-Claraco 379 Jun 23, 2022
cmake-font-lock - Advanced, type aware, highlight support for CMake

cmake-font-lock - Advanced, type aware, highlight support for CMake

Anders Lindgren 37 Dec 30, 2021
cmake-avr - a cmake toolchain for AVR projects

cmake-avr - a cmake toolchain for AVR projects Testing the example provided The toolchain was created and tested within the following environment: Lin

Matthias Kleemann 156 Jun 20, 2022
Make CMake less painful when trying to write Modern Flexible CMake

Izzy's eXtension Modules IXM is a CMake library for writing Modern flexible CMake. This means: Reducing the amount of CMake written Selecting reasonab

IXM 99 Feb 14, 2022
CMake module to enable code coverage easily and generate coverage reports with CMake targets.

CMake-codecov CMake module to enable code coverage easily and generate coverage reports with CMake targets. Include into your project To use Findcodec

HPC 76 Jun 1, 2022
unmaintained - CMake module to activate certain C++ standard, feature checks and appropriate automated workarounds - basically an improved version of cmake-compile-features

Compatibility This library provides an advanced target_compile_features() and write_compiler_detection_header(). The problem with those is that they a

Jonathan Müller 72 Apr 14, 2022
[CMake] [BSD-2] CMake module to find ICU

FindICU.cmake A CMake module to find International Components for Unicode (ICU) Library Note that CMake, since its version 3.7.0, includes a FindICU m

julp 29 Apr 26, 2022
Examples of using Hunter package manager to build and run Android application.

Examples of using Hunter package manager to build and run Android application. Requirements Android NDK Go to download page and choose NDK for your pl

null 34 Mar 13, 2022
CMake project for BL602 RISC-V processor

bl602_cmake_base CMake project for BL602 RISC-V processor How to build NOTE : This project uses a pre-compiled version of the Buffalo SDK (bl_iot_sdk)

null 9 Jan 6, 2022
A CMake toolchain file for iOS, macOS, watchOS & tvOS C/C++/Obj-C++ development

A CMake toolchain file for iOS, macOS, watchOS & tvOS C/C++/Obj-C++ development

Alexander Widerberg 1.3k Jun 29, 2022
curl cmake module libcurl build with msvc x86

curl-msvc Infomation curl cmake module libcurl build with MSVC10.0 arch (x86 | i386) source from https://github.com/curl/curl tags: curl-7_79_1 Usage

Jason Payne 0 May 16, 2022
NeoWorld is a resampler using the CMake build system

NeoWorld is a resampler using the CMake build system. It's designed for utsu, OpenUTAU, and UTAU.

null 4 Jun 24, 2022
A CMake addon that avoids you writing boilerplate code for resource management.

SHader INJ(I)ector SHINJI (originally SHader INJector) is a CMake addon that avoids you writing boilerplate code for resource management and exposes s

Lorenzo Rutayisire 6 Mar 21, 2022
A CMake starter template using CPM

Cmake Starter About A lightweight Cmake project meant for a binary application (not a shared library), tests with catch2 are configured, CPM is the pa

Matt Williams 1 Mar 24, 2022
Non-intrusive CMake dependency management

cmodule Non-intrusive CMake dependency management. Normally CMake's find_package() looks for packages installed on host system (and compiled for host

scapix.com 13 Jun 16, 2022
Simple library for embedding static resources into C++ binaries using CMake

libromfs libromfs is an easy way to bundle resources directly into any C++ application and access them through a simple interface. The main advantage

WerWolv 22 Apr 4, 2022