Search code examples
buildcmakeconan

Multiple conanfile.py management


Let's say I have 2 different conanfile.py in a project and I'm calling conan install two times to install their dependencies. I'm having trouble while adding them to cmake.

If I use basic setup

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

it only includes latest one. Is it possible to include multiple conanbuildinfo.cmake files ?


Solution

  • If you have 2 completely separate projects, you can have 2 different conanfiles and put the generated files in different folders:

    $ conan install conanfile1.py --install-folder=folder1
    $ conan install conanfile2.py --install-folder=folder2
    

    Then in your first project:

    include(<...>/folder1/conanbuildinfo.cmake)
    conan_basic_setup()
    

    And in your second project:

    include(<...>/folder2/conanbuildinfo.cmake)
    conan_basic_setup()
    

    You would need to define some consistent convention to locate the generated files for each project.

    Note, however, that if the different modules are intended to use together, like linked together lately, if you don't use the same dependencies and same versions, you will probably get linking or runtime errors in your global application. If the modules are related and you want to use the same versions of the dependencies, then you definitely want to use just 1 conanfile with all dependencies defined in it.

    Note that there are different ways to define the specific dependencies that you want, even if you use only 1 conanfile:

    • You can use the TARGETS of the cmake generator:
    include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
    conan_basic_setup(TARGETS)```
    
    add_library(mylib1 ... <sources>)
    target_link_libraries(mylib1 PUBLIC CONAN_PKG::Dep1 CONAN_PKG::Dep2)
    
    add_library(mylib2 ... <sources>)
    target_link_libraries(mylib2 PUBLIC CONAN_PKG::Dep3 CONAN_PKG::Dep4)
    
    • The cmake_find_package generators also generate one findXXXX.cmake file for each package in the dependency graph. You can use the find_package(XXXX) and later the results, specifying different dependencies. The cmake_find_package_multi generator is recommended.