cmake_paths generator

This generator is especially useful if you are using CMake based only on the find_package feature to locate the dependencies.

The cmake_paths generator creates a file named conan_paths.cmake declaring:

  • CMAKE_MODULE_PATH with the folders of the required packages, to allow CMake to locate the included cmake scripts and FindXXX.cmake files. The folder containing the conan_paths.cmake (self.install_folder when used in a recipe) is also included, so any custom file will be located too. Check cmake_find_package generator.

  • CMAKE_PREFIX_PATH used by FIND_LIBRARY() to locate library files (.a, .lib, .so, .dll) in your packages.

conanfile.txt
[requires]
zlib/1.2.11@conan/stable
...

[generators]
cmake_paths
CMakeList.txt
cmake_minimum_required(VERSION 3.0)
project(helloworld)
add_executable(helloworld hello.c)
find_package(Zlib)
if(ZLIB_FOUND)
   include_directories(${ZLIB_INCLUDE_DIRS})
   target_link_libraries (helloworld ${ZLIB_LIBRARIES})
endif()

In the example above, the zlib/1.2.11@conan/stable package is not packaging a custom FindZLIB.cmake file, but the FindZLIB.cmake included in the CMake installation directory (/Modules) will locate the zlib library from the Conan package because of the CMAKE_PREFIX_PATH used by the FIND_LIBRARY().

If the zlib/1.2.11@conan/stable would had included a custom FindZLIB.cmake in the package root folder or any declared self.cpp_info.builddirs, it would have been located because of the CMAKE_MODULE_PATH variable.

You can use the generated conan_paths.cmake file as a cmake toolchain or including it in a CMakeLists.txt of even including it in another toolchain:

Included as a toolchain

Without modifying your CMakeLists.txt file you can use the conan_paths.cmake as a toolchain:

 $ mkdir build && cd build
 $ conan install ..
 $ cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_paths.cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
 $ cmake --build .

Included using the CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE

With CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE you can specify a file to be included by the project() command. If you already have a toolchain file you can use this variable to include the conan_paths.cmake and insert your toolchain with the CMAKE_TOOLCHAIN_FILE.

$ mkdir build && cd build
$ conan install ..
$ cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_PROJECT_helloworld_INCLUDE=build/conan_paths.cmake
$ cmake --build .

Included in your CMakeLists.txt

CMakeList.txt
cmake_minimum_required(VERSION 3.0)
project(helloworld)
include(${CMAKE_BINARY_DIR}/conan_paths.cmake)
add_executable(helloworld hello.c)
find_package(Zlib)
if(ZLIB_FOUND)
   include_directories(${ZLIB_INCLUDE_DIRS})
   target_link_libraries (helloworld ${ZLIB_LIBRARIES})
endif()
$ mkdir build && cd build
$ conan install ..
$ cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
$ cmake --build .

See also

Check the section Reference/Generators/cmake_paths to read more about this generator.

Note

The CMAKE_MODULE_PATH and CMAKE_PREFIX_PATH contain the paths to the builddirs of every required package. By default, the root package folder is the only declared builddirs directory. Check the Reference/conanfile.py/attributes.