Search code examples
cmakeconan

Conan2: how to use CMakeDeps and still find target?


I've declared the following generator and requirement in my conan recipe:

generators = "CMakeDeps"

def requirements(self):
    self.requires("openssl/[>=1.1 <4]")

I also have a generate()-method in my recipe:

def generate(self):
    toolchain = CMakeToolchain(self)
    toolchain.generate()

The thing is, when using the "CMakeDeps" generator, then the build process fails and when removing it the build process succeeds. It fails saying that the openssl:openssl target cannot be found. But, the FindOpenSSL.cmake file is available in the generators folder inside the build folder.

Is it, that when removing the "CMakeDeps" generator, the FindOpenSSL.cmake file from "usr/share/cmake-3.16/Modules" is used? And how to tell CMake to use the FindOpenSSL.cmake file that is created by CMakeDeps.


Solution

  • The CMakeDeps generator will only generate all those Findxxx.cmake files, but will not tell to CMake where they are installed.

    To do that, the safest way is using CMakeToolchain generator together:

    generators = "CMakeDeps", "CMakeToolchain"
    

    Then you should tell CMake to use that generated file:

    conan install . --output-folder=build --build=missing
    cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=build/conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
    

    The conan_toolchain will include the CMake module path where those new cmake files are installed, and will use it instead of from your system.

    Please, try to follow the Conan 2.x tutorial first. This is a very basic instruction, detailed and explained there: https://docs.conan.io/2/tutorial/consuming_packages/build_simple_cmake_project.html