# protobuf\_generate\_cpp and fetchedconttent

**URL:** https://discourse.cmake.org/t/protobuf-generate-cpp-and-fetchedconttent/3644
**Category:** Usage
**Created:** [June 28, 2021, 4:08pm UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-and-fetchedconttent/3644 "2021-06-28T16:08:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![wannamak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/w/ecae2f/32.png) [@wannamak](https://discourse.cmake.org/u/wannamak)
#### Post date: [June 28, 2021, 4:08pm UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-and-fetchedconttent/3644/1 "2021-06-28T16:08:41Z")

</div>

Is it possible to FetchContent\_Declare and FetchContent\_MakeAvailable a Google protocol buffers version, then have find\_package(Protobuf) use the resulting built protoc and libraries? If I force find\_package to use the path to the built protoc, it says I need an alias. But, the lib and protoc are present and available.

---

<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: [June 28, 2021, 5:06pm UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-and-fetchedconttent/3644/2 "2021-06-28T17:06:57Z")

</div>

It sounds like protobuf isn’t set up to expose the right API for `FetchContent` (not all projects are prepared to be `add_subdirectory` into another project). I’m not sure what else would be needed on their side exactly.

Cc: @craig.scott

---

<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: [June 28, 2021, 10:57pm UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-and-fetchedconttent/3644/3 "2021-06-28T22:57:28Z")

</div>

`find_package()` usually requires things to have been built, but that hasn’t happened yet during the first configure run. If you use FetchContent to pull in protobuf, then you can’t use `find_package()` on it like that. Also, as you’ve discovered, you’ll probably end up with conflicting target names, since `find_package()` assumes it is adding the libraries rather than using the ones that will come in as a result of FetchContent pulling in protobuf to be built with the project.

Note that there is a [proposal](https://gitlab.kitware.com/cmake/cmake/-/issues/21687) which would probably make things work like how you hope, but I haven’t had the time to push it forward yet.

---

<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: [June 28, 2021, 10:58pm UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-and-fetchedconttent/3644/4 "2021-06-28T22:58:48Z")

</div>

Actually, can you just skip the call to `find_package()`? If FetchContent is already pulling in the project and it is providing the targets you need, the call to `find_package()` should be unnecessary (unless only some of the required targets are provided - I’m not familiar with the details of this particular dependency).

---

<div class="post-metadata">

### Author: ![wannamak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/w/ecae2f/32.png) [@wannamak](https://discourse.cmake.org/u/wannamak)
#### Post date: [June 28, 2021, 11:18pm UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-and-fetchedconttent/3644/5 "2021-06-28T23:18:08Z")

</div>

Thank you Craig and Ben. Craig, that is a great suggestion, and where I ended up. I skip find\_package, and reach down into the protobuf\_BINARY\_DIR for the protoc and libprotobuf.a it built, and down into protobuf\_SOURCE\_DIR for the necessary includes. Then I construct an add\_custom\_command to invoke protoc for my proto compile. It’s just that find\_package(Protobuf) / protobuf\_generate\_cpp could do the same thing and be much less verbose than the mess I now have ☹

---

<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: [June 28, 2021, 11:33pm UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-and-fetchedconttent/3644/6 "2021-06-28T23:33:59Z")

</div>

I think upstream might have `protobuf_generate(LANGS)` now if you can find where they stuff the API in the repo.
