The docs have been updated since the accepted answer was posted. The correct way to generate both Python and C++ files with protoc in Cmake is with the generic protobuf_generate command with different language arguments
# Write this again identically with a new language option
protobuf_generate(
LANGUAGE cpp
TARGET proto-objects
IMPORT_DIRS "${CMAKE_CURRENT_LIST_DIR}/backend/proto"
PROTOC_OUT_DIR "${PROTO_BINARY_DIR}"
APPEND_PATH
FILES ${PROTO_FILES}
)
My full implementation looks like:
# Config keyword is optional
find_package(Protobuf CONFIG)
# Glob check if you have many proto files
file(GLOB_RECURSE PROTO_FILES "${CMAKE_CURRENT_LIST_DIR}/backend/proto/*.proto")
add_library(proto-objects OBJECT ${PROTO_FILES})
target_link_libraries(proto-objects PUBLIC protobuf::libprotobuf)
set(PROTO_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/backend/proto/generated/")
file(MAKE_DIRECTORY ${PROTO_BINARY_DIR})
target_include_directories(proto-objects PUBLIC "$<BUILD_INTERFACE:${PROTO_BINARY_DIR}>")
protobuf_generate(
LANGUAGE cpp
TARGET proto-objects
IMPORT_DIRS "${CMAKE_CURRENT_LIST_DIR}/backend/proto"
PROTOC_OUT_DIR "${PROTO_BINARY_DIR}"
APPEND_PATH
FILES ${PROTO_FILES}
)
protobuf_generate(
LANGUAGE python
TARGET proto-objects
IMPORT_DIRS "${CMAKE_CURRENT_LIST_DIR}/backend/proto"
PROTOC_OUT_DIR "${PROTO_BINARY_DIR}"
APPEND_PATH
FILES ${PROTO_FILES}
)
target_link_libraries(target_name PUBLIC
$<TARGET_OBJECTS:proto-objects>
)
Docs : https://github.com/protocolbuffers/protobuf/blob/main/docs/cmake_protobuf_generate.md
And yeah, the offical FindProtobuf docs are out of date
protobuf_generate_pythondoes exist inFindProtobuf.cmake, so perhaps you aren't using it correctly?