# FetchContent issue with find\_package and dependency chains

**URL:** <https://discourse.cmake.org/t/fetchcontent-issue-with-find-package-and-dependency-chains/8061>\
**Category:** Code\
**Tags:** os:windows, gen:vs\
**Created:** [May 5, 2023, 3:30pm UTC](https://discourse.cmake.org/t/fetchcontent-issue-with-find-package-and-dependency-chains/8061 "2023-05-05T15:30:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yah-nosh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/y/ce7236/32.png) [@yah-nosh](https://discourse.cmake.org/u/yah-nosh)\
**Post date:** [May 5, 2023, 3:30pm UTC](https://discourse.cmake.org/t/fetchcontent-issue-with-find-package-and-dependency-chains/8061/1 "2023-05-05T15:30:39Z")

</div>

Hi all,

I’m working on a C++ project using CMake, where I manage all my third-party dependencies using FetchContent exclusively (i.e no other package manager is installed nor run).

I use a combination of `FetchContent_Declare` and `FetchContent_Populate` (as outlined in the documentation [here](https://cmake.org/cmake/help/latest/module/FetchContent.html#overview)), as so far this has worked without any problems for dependencies that are independent from each other, i.e only my own code required them (this includes libraries like SDL2, Dear ImGui, and glm)

Recently, I tried the same approach with [msdf-atlas-gen](https://github.com/Chlumsky/msdf-atlas-gen). This library uses [FreeType](https://github.com/freetype/freetype), which in turn uses [zlib](https://github.com/madler/zlib) and [libpng](https://github.com/glennrp/libpng). Using the above approach, I added all dependencies, making sure to fetch them in the correct order so each dependency would be ready by the time their dependents were being fetched.

This did not work at first. The fetch succeeded, but the calls to `find_package` in the CMake scripts of the above libraries could not find their dependencies. I then found [this](https://cmake.org/cmake/help/latest/module/FetchContent.html#integrating-with-find-package) part of the documentation, and using `OVERRIDE_FIND_PACKAGE` and `FetchContent_MakeAvailable`, I was able to run the entire CMake script with no apparent issues (i.e no more error messages from `find_package`)

When I try to build the project, however, anything below zlib in the dependency chain is unable to find the zlib headers, and a similar issue happens with the dependencies that require libpng. When generating with Visual Studio, I checked the vcxproj files generated by CMake, and it seems neither the include directories, nor the libraries themselves were added.

I attached a minimal reproduction of the problem. I primarily use Visual Studio 2022 as my IDE and generator, though I have tested with Ninja as well. Am I missing something? Or is this an inherent limitation of FetchContent, and/or a problem with how the CMake scripts of the dependencies are configured?  
[FetchContentTest.zip](https://discourse.cmake.org/uploads/short-url/uea2YcQTkoKiuyCwrMyCtr24N9M.zip) (3.0 KB)

---

<div class="post-metadata">

**Author:** ![menno](https://discourse.cmake.org/user_avatar/discourse.cmake.org/menno/32/2928_2.png) [@menno](https://discourse.cmake.org/u/menno)\
**Post date:** [May 10, 2023, 8:43am UTC](https://discourse.cmake.org/t/fetchcontent-issue-with-find-package-and-dependency-chains/8061/2 "2023-05-10T08:43:49Z")

</div>

If you are unable to solve the problem of FetchContent, you could alternatively try superbuild, see [https://gitlab.kitware.com/paraview/common-superbuild](https://gitlab.kitware.com/paraview/common-superbuild)

We’re managing a reasonably complex build with several third party interdependencies with it. And ParaView is also built with it of course.

---

<div class="post-metadata">

**Author:** ![assignUser](https://discourse.cmake.org/user_avatar/discourse.cmake.org/assignuser/32/3367_2.png) [@assignUser](https://discourse.cmake.org/u/assignUser)\
**Post date:** [May 31, 2023, 6:25pm UTC](https://discourse.cmake.org/t/fetchcontent-issue-with-find-package-and-dependency-chains/8061/3 "2023-05-31T18:25:20Z")

</div>

A number of CMakeLists.txt still rely on `<PACKAGE>_INCLUDE_DIRS` and `<PACKAGE>_LIBRARIES` instead of using targets, these are set by the find-module or config but not by the project it self so they are missing here → empty var = ‘’

Looking at [freetype/CMakeLists.txt at master · freetype/freetype · GitHub](https://github.com/freetype/freetype/blob/master/CMakeLists.txt#L525) this is the case. You will have to `set(ZLIB_LIBRARIES zlib)` manually (adding and using a zlib::zlib alias would probably also be good). You can ignore the include dirs as that will be handled by the target.

p.s. If projects are using namespaced targets that are not also created in the project cmake (e.g. zlib::zlib) you will have to add those too.
