How to install your custom CMake-Find module

CmakeCpack

Cmake Problem Overview


I configure and package my library using CMake and CPack. I have written my own find-module: FindMyLib.cmake.

How do I tell CMake/CPack to add this file to the CMake module directory, so that future developers can simply specify FIND_PACKAGE(MyLib) to use my library?

Cmake Solutions


Solution 1 - Cmake

You can set CMAKE_MODULE_PATH and distribute your custom FindFoo.cmake with your project. For example:

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

Solution 2 - Cmake

The CMake module directory is part of the install tree of CMake itself, and as such you shouldn't be trying to add anything there.

The CMake module directory contains modules which have been written or at least reviewed by Kitware, and adding your own there would give the impression to users of your project that this was the case for your project also.

You'd be better to just install FindMyLib.cmake to one of the places searched by find_package:

> / (Windows) > /(cmake|CMake)/ (Windows) > // (Windows) > //(cmake|CMake)/ (Windows) > /(lib/|lib|share)/cmake// (Unix) > /(lib/|lib|share)// (Unix) > /(lib/|lib|share)//(cmake|CMake)/ (Unix) > /.framework/Resources/ (Apple) > /.framework/Resources/CMake/ (Apple) > /.framework/Versions//Resources/ (Apple) > /.framework/Versions/*/Resources/CMake/ (Apple) > /.app/Contents/Resources/ (Apple) > /.app/Contents/Resources/CMake/ (Apple)


See the documentation for find_package for the full details of how find_package searches. Also the CMake packaging tutorial is useful in this case.

Solution 3 - Cmake

The best way to allow > future developers can simply specify FIND_PACKAGE(MyLib) to use my library

is to write a package config file (-config.cmake) , not a Find module. The package config file should then be installed in one of the folders where the FindPackage module looks for (something like /lib/package/ or /lib/cmake/package the second being preferred)

The FindPackage module will automatically load the config file if it can find it there.

The CMake wiki has more detailed instructions at https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/Packaging

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongoocreationsView Question on Stackoverflow
Solution 1 - CmakesimontView Answer on Stackoverflow
Solution 2 - CmakeFraserView Answer on Stackoverflow
Solution 3 - CmakeTriskeldeianView Answer on Stackoverflow