Add dependencies to packages

In the previous tutorial section we created a Conan package for a “Hello World” C++ library. We used the conan.tools.scm.Git() tool to retrieve the sources from a git repository. So far, the package does not have any dependency on other Conan packages. Let’s explain how to add a dependency to our package in a very similar way to how we did in the consuming packages section. We will add some fancy colour output to our “Hello World” library using the fmt library.

Please, first clone the sources to recreate this project. You can find them in the examples2 repository on GitHub:

  1. $ git clone https://github.com/conan-io/examples2.git
  2. $ cd examples2/tutorial/creating_packages/add_requires

You will notice some changes in the conanfile.py file from the previous recipe. Let’s check the relevant parts:

  1. ...
  2. from conan.tools.build import check_max_cppstd, check_min_cppstd
  3. ...
  4. class helloRecipe(ConanFile):
  5. name = "hello"
  6. version = "1.0"
  7. ...
  8. generators = "CMakeDeps"
  9. ...
  10. def validate(self):
  11. check_min_cppstd(self, "11")
  12. check_max_cppstd(self, "20")
  13. def requirements(self):
  14. self.requires("fmt/8.1.1")
  15. def source(self):
  16. git = Git(self)
  17. git.clone(url="https://github.com/conan-io/libhello.git", target=".")
  18. # Please, be aware that using the head of the branch instead of an immutable tag
  19. # or commit is not a good practice in general
  20. git.checkout("require_fmt")
  • First, we set the generators class attribute to make Conan invoke the CMakeDeps generator. This was not needed in the previous recipe as we did not have dependencies. CMakeDeps will generate all the config files CMake needs to find the fmt library.

  • Next, we use the requires() method to add the fmt dependency to our package.

  • Also, check that we added an extra line in the source() method. We use the Git().checkout method to checkout the source code in the require_fmt branch. This branch contains the changes in the source code to add colours to the library messages, and also in the CMakeLists.txt to declare that we are using the fmt library.

  • Finally, note we added the validate() method to the recipe. We already used this method in the consuming packages section to raise an error for non-supported configurations. Here, we call the check_min_cppstd() and check_max_cppstd() to check that we are using at least C++11 and at most C++20 standards in our settings.

You can check the new sources, using the fmt library in the require_fmt. You will see that the hello.cpp file adds colours to the output messages:

  1. #include <fmt/color.h>
  2. #include "hello.h"
  3. void hello(){
  4. #ifdef NDEBUG
  5. fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, "hello/1.0: Hello World Release!\n");
  6. #else
  7. fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, "hello/1.0: Hello World Debug!\n");
  8. #endif
  9. ...

Let’s build the package from sources with the current default configuration, and then let the test_package folder test the package. You should see the output messages with colour now:

  1. $ conan create . --build=missing
  2. -------- Exporting the recipe ----------
  3. ...
  4. -------- Testing the package: Running test() ----------
  5. hello/1.0 (test package): Running test()
  6. hello/1.0 (test package): RUN: ./example
  7. hello/1.0: Hello World Release!
  8. hello/1.0: __x86_64__ defined
  9. hello/1.0: __cplusplus 201103
  10. hello/1.0: __GNUC__ 4
  11. hello/1.0: __GNUC_MINOR__ 2
  12. hello/1.0: __clang_major__ 13
  13. hello/1.0: __clang_minor__ 1
  14. hello/1.0: __apple_build_version__ 13160021

See also