# protobuf\_generate() seems to be generating incorrect paths. Usage error or bug?

**URL:** <https://discourse.cmake.org/t/protobuf-generate-seems-to-be-generating-incorrect-paths-usage-error-or-bug/15112>\
**Category:** Code\
**Tags:** os:linux, gen:ninja\
**Created:** [August 25, 2025, 7:29pm UTC](https://discourse.cmake.org/t/protobuf-generate-seems-to-be-generating-incorrect-paths-usage-error-or-bug/15112 "2025-08-25T19:29:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![GarrettK](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/9e8a1a/32.png) [@GarrettK](https://discourse.cmake.org/u/GarrettK)\
**Post date:** [August 25, 2025, 7:29pm UTC](https://discourse.cmake.org/t/protobuf-generate-seems-to-be-generating-incorrect-paths-usage-error-or-bug/15112/1 "2025-08-25T19:29:07Z")

</div>

I’m working on a larger project which currently performs in-tree out-of-build protobuf generation. I’m looking at converting to using in-build protobuf generation with CMake’s support. Unfortunately, I’m running into issues where protobuf\_generate() seems to attempt to use incorrect paths. Hopefully this is a usage issue. If not, I can see about filing a ticket.

I’m currently using cmake 3.30.5

I’ve created a minimal testcase with the directory hierarchy:

- builds
- libs
- 
  - proto\_build

- 
  - 
    - CMakeLists.txt

- protos
- 
  - proto-base

- 
  - 
    - proto\_component

- 
  - 
    - 
      - test.proto

- CMakeLists.txt

The contents:  
CMakeLists.txt:

```auto
cmake_minimum_required(VERSION 3.22.1)
project(cmake_demo VERSION 0.0.1)
set(ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
add_subdirectory(libs/proto_build)

```

libs/proto\_build/CMakeLists.txt:

```auto
project(ProtoBuildProject)
find_package(Qt6 COMPONENTS Core Network REQUIRED)
find_package(Protobuf)
file(GLOB_RECURSE PROTO_FILES "${ROOT_DIR}/protos/proto-base/proto_component/*.proto")
add_library(proto-build-proto-lib)
protobuf_generate(
    TARGET proto-build-proto-lib
    LANGUAGE cpp
    OUT_VAR BUILD_PROTO_OUT_FILES
    EXPORT_MACRO "EXPORT_MACRO"
\# PROTOC_OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/proto/build/src"
    PROTOC_OUT_DIR "${CMAKE_CURRENT_BINARY_DIR}"
   PROTOS ${PROTO_FILES}
    IMPORT_DIRS "${ROOT_DIR}/protos/"
)

```

protos/proto-base/proto\_component/test.proto:

```auto
syntax = "proto3";
package proto_component;
message TestProto
{
    string s = 1;
    int32 i = 2;
}

```

I run cmake with:

```auto
/home/developer/Qt/Tools/CMake/bin/cmake -S /home/developer/cmake_demo -B /home/developer/cmake_demo/builds/Debug -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc -DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ -DCMAKE_GENERATOR:STRING=Ninja

```

My expectation is that I would have the relevant .pb.cc and .pb.h files created, compiled and linked into a library. Instead, I end up with path mismatches.

First, the files are created. They can be found at:

- /home/developer/cmake\_demo/builds/Debug/libs/proto\_build/proto-base/proto\_component/test.pb.cc
- /home/developer/cmake\_demo/builds/Debug/libs/proto\_build/proto-base/proto\_component/test.pb.h

However, the attempt to build them fails. I get:

> $ ninja  
> FAILED: libs/proto\_build/CMakeFiles/proto-build-proto-lib.dir/ **/** /protos/proto-base/proto\_component/test.pb.cc.o  
> /usr/bin/g++ -O2 -pipe -g -feliminate-unused-debug-types -g -MD -MT libs/proto\_build/CMakeFiles/proto-build-proto-lib.dir/ **/** /protos/proto-base/proto\_component/test.pb.cc.o -MF libs/proto\_build/CMakeFiles/proto-build-proto-lib.dir/ **/** /protos/proto-base/proto\_component/test.pb.cc.o.d -o libs/proto\_build/CMakeFiles/proto-build-proto-lib.dir/ **/** /protos/proto-base/proto\_component/test.pb.cc.o -c /home/developer/cmake\_demo/builds/Debug/protos/proto-base/proto\_component/test.pb.cc  
> cc1plus: fatal error: /home/developer/cmake\_demo/builds/Debug/protos/proto-base/proto\_component/test.pb.cc: No such file or directory

So it looks like it builds the .pb.cc/.pb.h files in the specified PROTOC\_OUT\_DIR directory, but then looks for them to be in the source directory.

As a related issue, I noticed that the PROTOC\_OUT\_DIR parameter doesn’t work if the specified directory doesn’t exist. If I swap out the commented-out version above to place the generated files in subdirectory of ${CMAKE\_CURRENT\_BINARY\_DIR} which currently doesn’t exist, the build will fail due to the directory not existing.

Please let me know if I’ve done something obviously-wrong here which can be worked around.

---

<div class="post-metadata">

**Author:** ![vito.gamberini](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vito.gamberini/32/4376_2.png) [@vito.gamberini](https://discourse.cmake.org/u/vito.gamberini)\
**Post date:** [August 26, 2025, 2:34am UTC](https://discourse.cmake.org/t/protobuf-generate-seems-to-be-generating-incorrect-paths-usage-error-or-bug/15112/2 "2025-08-26T02:34:38Z")

</div>

I don’t have the time at the moment to fully debug your example, but I’ve done a hello-world example for `protobuf_generate()` in the context of gRPC before that may be useful to you.

> **[GitHub - nickelpro/grpc-example](https://github.com/nickelpro/grpc-example)**
>
> Contribute to nickelpro/grpc-example development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![GarrettK](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/9e8a1a/32.png) [@GarrettK](https://discourse.cmake.org/u/GarrettK)\
**Post date:** [August 27, 2025, 4:34pm UTC](https://discourse.cmake.org/t/protobuf-generate-seems-to-be-generating-incorrect-paths-usage-error-or-bug/15112/3 "2025-08-27T16:34:21Z")

</div>

Hi!

Thanks for the response. I suspect that the issue is related to the proto files being in a different directory tree from the child CMakeLists.txt file leading to path generation issues. The example you provided seems to have the code organized in simpler and more coherent structure than the one I am dealing with.
