# protobuf using pkgconfig, how to generate files into a target build tree?

**URL:** <https://discourse.cmake.org/t/protobuf-using-pkgconfig-how-to-generate-files-into-a-target-build-tree/15336>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [November 21, 2025, 10:47am UTC](https://discourse.cmake.org/t/protobuf-using-pkgconfig-how-to-generate-files-into-a-target-build-tree/15336 "2025-11-21T10:47:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MickeJ](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/a5b964/32.png) [@MickeJ](https://discourse.cmake.org/u/MickeJ)\
**Post date:** [November 21, 2025, 10:47am UTC](https://discourse.cmake.org/t/protobuf-using-pkgconfig-how-to-generate-files-into-a-target-build-tree/15336/1 "2025-11-21T10:47:23Z")

</div>

Hi, due to project requirement only pkgconfig third party packages can be used, and cmake find\_package(Protobuf) and protobuf\_generate\_cpp() does not work when using pkgconfig-target defined protobuf.

How do i make a target with protoc generated header files and export a link to its build tree generated files? And how do i make the generator run before depent target and only run when .proto file is changed?

I have below design idea that i need help with.

```auto
add_library(proto_def)

add_custom_target(proto_def-out ALL
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/out
COMMENT “Creates protobuf sources: ${CMAKE_BINARY_DIR}/out”
#WORKING_DIRECTORY “${_BINARY_DIR}”
COMMAND protoc --cpp_out=${CMAKE_CURRENT_BINARY_DIR}/out .proto
)

add_custom_command(OUTPUT
COMMAND protoc --cpp_out=${CMAKE_CURRENT_BINARY_DIR}/out
COMMENT “Generating proto headers.”
VERBATIM
)

TARGET_SOURCES(proto_def PUBLIC
“Some gen expression pointing to build tree target proto gen out files”
)

add_dependencies(proto_def-out proto_def)
target_link_libraries(proto_def-out proto-pkgconfig)

```

This is the parent and works.

```auto
add_library(proto-pkgconfig)

set_target_properties (proto-pkgconfig PROPERTIES
        EXPORT_COMPILE_COMMANDS ON
        DEBUG_POSTFIX _debug
)

target_compile_features(proto-pkgconfig PRIVATE cxx_std_23)

target_sources(proto-pkgconfig
        PUBLIC
        proto.cpp
        PUBLIC
        FILE_SET HEADERS
)

find_package(PkgConfig REQUIRED)
pkg_check_modules(${PROJECT_NAME}-protoPkgconfig REQUIRED IMPORTED_TARGET protobuf)
target_link_libraries(proto-pkgconfig PRIVATE PkgConfig::${PROJECT_NAME}-protoPkgconfig)

add_subdirectory(proto_def)

```

---

<div class="post-metadata">

**Author:** ![MickeJ](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/a5b964/32.png) [@MickeJ](https://discourse.cmake.org/u/MickeJ)\
**Post date:** [November 22, 2025, 2:28pm UTC](https://discourse.cmake.org/t/protobuf-using-pkgconfig-how-to-generate-files-into-a-target-build-tree/15336/2 "2025-11-22T14:28:02Z")

</div>

Solved below works, made a common interface target instead of two separate targets.

```auto
#Target name based on directory name
cmake_path(GET CMAKE_CURRENT_LIST_DIR FILENAME TARGET-NAME)

add_library(${TARGET-NAME} INTERFACE)
add_library(${PROJECT_NAME}::${TARGET-NAME} ALIAS ${TARGET-NAME})

add_custom_target(${TARGET-NAME}_gen ALL
        COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/out
        COMMAND protoc --proto_path=${CMAKE_CURRENT_SOURCE_DIR} .proto --cpp_out=${CMAKE_CURRENT_BINARY_DIR}/out
        DEPENDS ${CMAKE_CURRENT_SOURCE_DIR} .proto
        COMMENT "Generates header from proto file"
)

target_sources(${TARGET-NAME}
        PUBLIC
        FILE_SET HEADERS
        BASE_DIRS "$<BUILD_LOCAL_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/out>"
)

add_dependencies(${TARGET-NAME} ${TARGET-NAME}_gen)

find_package(PkgConfig REQUIRED)
pkg_check_modules(protobuf REQUIRED IMPORTED_TARGET protobuf)
target_link_libraries(${TARGET-NAME} INTERFACE PkgConfig::protobuf)

```
