# What are the interaction guarantees with find\_package and FetchContent\_\*

**URL:** https://discourse.cmake.org/t/what-are-the-interaction-guarantees-with-find-package-and-fetchcontent/10113
**Category:** Usage
**Created:** [February 16, 2024, 9:58am UTC](https://discourse.cmake.org/t/what-are-the-interaction-guarantees-with-find-package-and-fetchcontent/10113 "2024-02-16T09:58:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![etiennemlb](https://discourse.cmake.org/user_avatar/discourse.cmake.org/etiennemlb/32/3147_2.png) [@etiennemlb](https://discourse.cmake.org/u/etiennemlb)
#### Post date: [February 16, 2024, 9:58am UTC](https://discourse.cmake.org/t/what-are-the-interaction-guarantees-with-find-package-and-fetchcontent/10113/1 "2024-02-16T09:58:14Z")

</div>

Say, in a large library where not all the dependencies are under the explicit control of someone or some standard, I have:

```auto
find_package(xxx VERSION 1.0 REQUIRED) followed, say in a very remote library, by a
FetchContent_Declare(xxx SYSTEM OVERRIDE_FIND_PACKAGE)
FetchContent_MakeAvailable(xxx)

target_link_libraries(abc PRIVATE xxx)

```

What `xxx` lib will be used for `abc`, the one first found using `find_package` or the one via `FetchContent_MakeAvailable`.

If we append the following lin to the example:

```auto
find_package(xxx VERSION 1.0 REQUIRED)
target_link_libraries(efg PRIVATE xxx)

```

What `xxx` lib will be used for `efg`, the one first found using first `find_package` or the one via `FetchContent_MakeAvailable` (because we used `OVERRIDE_FIND_PACKAGE`).

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 18, 2024, 3:35am UTC](https://discourse.cmake.org/t/what-are-the-interaction-guarantees-with-find-package-and-fetchcontent/10113/2 "2024-02-18T03:35:20Z")

</div>

CMake doesn’t support two targets with the same name to exist within a scope. Note that `find_package` bits will usually check for existence before making their own targets (to avoid errors when called multiple times within a scope). Projects usually assume that nothing else will provide its own target names.

So, the “typical” answer is that `find_package` first will probably cause a duplicate target error from the `FetchContent`. Switching the order will make the package script not make targets because they already exist.

The true answer can only be discovered by looking at the code (or using `--trace-expand` to see where targets are made).

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 23, 2024, 3:26am UTC](https://discourse.cmake.org/t/what-are-the-interaction-guarantees-with-find-package-and-fetchcontent/10113/3 "2024-03-23T03:26:35Z")

</div>

Sorry it took me a while to get to this question. The example in your original post is malformed. If you use the `OVERRIDE_FIND_PACKAGE` keyword with `FetchContent_Declare()`, you must ensure that call occurs before any call to `find_package()` for that same dependency. This isn’t explicitly stated in the FetchContent docs, but those docs do say that when using the `OVERRIDE_FIND_PACKAGE` keyword, it only affects _subsequent_ calls to `find_package()` for that dependency.
