# PROTOBUF\_GENERATE\_CPP does not generating src and header files

**URL:** https://discourse.cmake.org/t/protobuf-generate-cpp-does-not-generating-src-and-header-files/9893
**Category:** Usage
**Tags:** os:linux, tool:cmake
**Created:** [January 21, 2024, 3:08pm UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-does-not-generating-src-and-header-files/9893 "2024-01-21T15:08:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![black\_anime](https://discourse.cmake.org/user_avatar/discourse.cmake.org/black_anime/32/4210_2.png) [@black\_anime](https://discourse.cmake.org/u/black_anime)
#### Post date: [January 21, 2024, 3:08pm UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-does-not-generating-src-and-header-files/9893/1 "2024-01-21T15:08:35Z")

</div>

Directory:

```auto
|test
--|build
--|msg
--|proto
----|test.poto
--|CMakeLists.txt
--|test.bat

```

this is my CMakeLists.txt

```auto
cmake_minimum_required(VERSION 3.0.0)

project(test)
message(STATUS "source path: ${PROJECT_SOURCE_DIR}")

file(GLOB PROTO_FILE proto/*.proto)
if(NOT WIN32)
    message("if NOT WIN32")
    message("NOT WIN32 ${PROTO_FILE}---------------")
    find_package(Protobuf REQUIRED)
    SET_SOURCE_FILES_PROPERTIES(${PROTO_SRC} ${PROTO_INCL} PROPERTIES GENERATED TRUE)
    #include_directories(${Protobuf_INCLUDE_DIRS})
    #include_directories(${CMAKE_CURRENT_BINARY_DIR})
    # PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS ${PROTO_FILE})
    protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ./proto/test.proto)
else(NOT WIN32)
    message("else NOT WIN32 ${PROTO_FILE}---------------")
    foreach(proto ${PROTO_FILE})
        execute_process(
            COMMAND protoc --cpp_out=./ --proto_path=${CMAKE_CURRENT_SOURCE_DIR} ${proto}
            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
        )
    endforeach()
    # message("else NOT WIN32 ${PROTO_FILE}---------------")
    # execute_process(
    # COMMAND cmd /c "test.bat"
    # WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    # RESULT_VARIABLE CMD_RESULT
    # )

    # if (CMD_RESULT EQUAL 0)
    # message("Script executed successfully.")
    # else()
    # message("Script execution failed with error code: ${CMD_RESULT}")
    # endif()
endif()

```

in buid i excute cmake … output:

```auto
-- source path: /data/cmake
if NOT WIN32
NOT WIN32 /data/cmake/proto/test.proto---------------
-- Configuring done
-- Generating done
-- Build files have been written to: /data/cmake/build

```

but it does not generating src and header files i try like:add a line SET\_SOURCE\_FILES\_PROPERTIES(${PROTO\_SRC} ${PROTO\_INCL} PROPERTIES GENERATED TRUE) not work  
But i excute protoc -I ./ \*.proto --cpp\_out=./ in proto it succ,so i wanna know why PROTOBUF\_GENERATE\_CPP not generating src and header files

**trys:**  
add a line  
SET\_SOURCE\_FILES\_PROPERTIES(${PROTO\_SRC} ${PROTO\_INCL} PROPERTIES GENERATED TRUE)  
not work  
i see this:  
[PROTOBUF\_GENERATE\_CPP not generating src and header files](https://stackoverflow.com/questions/29346488/protobuf-generate-cpp-not-generating-src-and-header-files)  
[cmake protobuf\_generate\_cpp not invoking protoc](https://stackoverflow.com/questions/75728562/cmake-protobuf-generate-cpp-not-invoking-protoc) **wants:**  
PROTOBUF\_GENERATE\_CPP generate src and header files

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [January 22, 2024, 1:46pm UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-does-not-generating-src-and-header-files/9893/2 "2024-01-22T13:46:33Z")

</div>

You’re only showing CMake output. The files are supposed to be generated at build time (when building), not when CMake is running. Are the files generated when you do a build of the CMake-generated buildsystem?

---

<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: [January 22, 2024, 3:02pm UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-does-not-generating-src-and-header-files/9893/3 "2024-01-22T15:02:15Z")

</div>

The generated sources also need to be added to a target to compile as there’s nothing tying the custom command output to the default `all` target either (IIRC).

---

<div class="post-metadata">

### Author: ![Mohsen\_Abdolahzadeh](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mohsen_abdolahzadeh/32/5085_2.png) [@Mohsen\_Abdolahzadeh](https://discourse.cmake.org/u/Mohsen_Abdolahzadeh)
#### Post date: [November 3, 2024, 5:24am UTC](https://discourse.cmake.org/t/protobuf-generate-cpp-does-not-generating-src-and-header-files/9893/4 "2024-11-03T05:24:22Z")

</div>

I have the same problem, The files are not generated in cmake configure and build time. How can I solve it?
