'How to properly use CMake subprojects together with Conan?

Let's say we have:

  • App CMake project which uses Conan to manage dependencies (via conanfile.txt)
  • Lib CMake library standalone project which also uses Conan for same purpose, lives in a separate repository
  • Lib is a subproject of App
  • Some of App and Lib dependencies in their own conanfile.txt files are same (let's say they both use boost/1.78.0)

In this case, App project fails to build with error:

[cmake] CMake Error at build/conanbuildinfo.cmake:1507 (add_library):
[cmake] add_library cannot create imported target "CONAN_LIB::boost_boost_contract"
[cmake] because another target with the same name already exists.

What's the proper way to handle such cases? When we have nested CMake subproject and both parent and nested project use Conan inside.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source