cmake_find_package generator

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

The cmake_find_package generator creates a file for each requirement specified in a conanfile.

The name of the files follows the pattern Find<package_name>.cmake. So for the zlib/1.2.11 package, a FindZLIB.cmake file will be generated.

In a conanfile.py

conanfile.py
from conans import ConanFile, CMake, tools


class LibConan(ConanFile):
    ...
    requires = "zlib/1.2.11"
    generators = "cmake_find_package"

    def build(self):
        cmake = CMake(self) # it will find the packages by using our auto-generated FindXXX.cmake files
        cmake.configure()
        cmake.build()

In the previous example, the CMake build helper will automatically adjust the CMAKE_MODULE_PATH to the conanfile.install_folder, where the generated Find<package_name>.cmake is.

In the CMakeList.txt you do not need to specify or include anything related with Conan at all; just rely on the find_package feature:

CMakeList.txt
cmake_minimum_required(VERSION 3.0)
project(helloworld)
add_executable(helloworld hello.c)
find_package(ZLIB)

# Global approach
if(ZLIB_FOUND)
   include_directories(${ZLIB_INCLUDE_DIRS})
   target_link_libraries (helloworld ${ZLIB_LIBRARIES})
endif()

# Modern CMake targets approach
if(TARGET ZLIB::ZLIB)
   target_link_libraries(helloworld ZLIB::ZLIB)
endif()
$ conan create . user/channel

lib/1.0@user/channel: Calling build()
-- The C compiler identification is AppleClang 9.1.0.9020039
...
-- Conan: Using autogenerated FindZLIB.cmake
-- Found: /Users/user/.conan/data/zlib/1.2.11/_/_/package/0eaf3bfbc94fb6d2c8f230d052d75c6c1a57a4ce/lib/libz.a
lib/1.0@user/channel: Package '72bce3af445a371b892525bc8701d96c568ead8b' created

In a conanfile.txt

If you are using a conanfile.txt file in your project, instead of a conanfile.py, this generator can be used together with the cmake_paths generator to adjust the CMAKE_MODULE_PATH and CMAKE_PREFIX_PATH variables automatically and let CMake locate the generated Find<package_name>.cmake files.

With cmake_paths:

conanfile.txt
[requires]
zlib/1.2.11
...

[generators]
cmake_find_package
cmake_paths
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)

# Global approach
if(ZLIB_FOUND)
   include_directories(${ZLIB_INCLUDE_DIRS})
   target_link_libraries (helloworld ${ZLIB_LIBRARIES})
endif()

# Modern CMake targets approach
if(TARGET ZLIB::ZLIB)
   target_link_libraries(helloworld ZLIB::ZLIB)
endif()
$ mkdir build && cd build
$ conan install ..
$ cmake .. -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release
  -- Conan: Using autogenerated FindZLIB.cmake
  -- Found: /Users/user/.conan/data/zlib/1.2.11/_/_/package/0eaf3bfbc94fb6d2c8f230d052d75c6c1a57a4ce/lib/libz.a
  ...

$ cmake --build .

Or you can also adjust CMAKE_MODULE_PATH and CMAKE_PREFIX_PATH manually.

Without cmake_paths, adjusting the variables manually:

conanfile.txt
 [requires]
 zlib/1.2.11
 ...

 [generators]
 cmake_find_package
CMakeList.txt
 cmake_minimum_required(VERSION 3.0)
 project(helloworld)
 set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR} ${CMAKE_MODULE_PATH})
 set(CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR} ${CMAKE_PREFIX_PATH})

 add_executable(helloworld hello.c)
 find_package(ZLIB)

 # Global approach
 if(ZLIB_FOUND)
    include_directories(${ZLIB_INCLUDE_DIRS})
    target_link_libraries (helloworld ${ZLIB_LIBRARIES})
 endif()

 # Modern CMake targets approach
 if(TARGET ZLIB::ZLIB)
    target_link_libraries(helloworld ZLIB::ZLIB)
 endif()

See also

Check the section cmake_find_package to read more about this generator and the adjusted CMake variables/targets.