Locate and configure the Google Protocol Buffers library.
Added in version 3.6: Support for find_package() version checks.
Changed in version 3.6: All input and output variables use the Protobuf_ prefix.
Variables with PROTOBUF_ prefix are still supported for compatibility.
The following variables can be set and are optional:
Protobuf_SRC_ROOT_FOLDERWhen compiling with MSVC, if this cache variable is set the protobuf-default VS project build locations (vsprojects/Debug and vsprojects/Release or vsprojects/x64/Debug and vsprojects/x64/Release) will be searched for libraries and binaries.
Protobuf_IMPORT_DIRSList of additional directories to be searched for imported .proto files.
Protobuf_DEBUGAdded in version 3.6.
Show debug messages.
Protobuf_USE_STATIC_LIBSAdded in version 3.9.
Set to ON to force the use of the static libraries. Default is OFF.
Defines the following variables:
Protobuf_FOUNDFound the Google Protocol Buffers library (libprotobuf & header files)
Protobuf_VERSIONAdded in version 3.6.
Version of package found.
Protobuf_INCLUDE_DIRSInclude directories for Google Protocol Buffers
Protobuf_LIBRARIESThe protobuf libraries
Protobuf_PROTOC_LIBRARIESThe protoc libraries
Protobuf_LITE_LIBRARIESThe protobuf-lite libraries
Added in version 3.9: The following IMPORTED targets are also defined:
protobuf::libprotobufThe protobuf library.
protobuf::libprotobuf-liteThe protobuf lite library.
protobuf::libprotocThe protoc library.
protobuf::protocAdded in version 3.10: The protoc compiler.
The following cache variables are also available to set or use:
Protobuf_LIBRARYThe protobuf library
Protobuf_PROTOC_LIBRARYThe protoc library
Protobuf_INCLUDE_DIRThe include directory for protocol buffers
Protobuf_PROTOC_EXECUTABLEThe protoc compiler
Protobuf_LIBRARY_DEBUGThe protobuf library (debug)
Protobuf_PROTOC_LIBRARY_DEBUGThe protoc library (debug)
Protobuf_LITE_LIBRARYThe protobuf lite library
Protobuf_LITE_LIBRARY_DEBUGThe protobuf lite library (debug)
Example:
find_package(Protobuf REQUIRED)
include_directories(${Protobuf_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS foo.proto)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS EXPORT_MACRO DLL_EXPORT foo.proto)
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS DESCRIPTORS PROTO_DESCS foo.proto)
protobuf_generate_python(PROTO_PY foo.proto)
add_executable(bar bar.cc ${PROTO_SRCS} ${PROTO_HDRS})
target_link_libraries(bar ${Protobuf_LIBRARIES})
Note
The protobuf_generate_cpp and protobuf_generate_python
functions and add_executable() or add_library()
calls only work properly within the same directory.
Add custom commands to process .proto files to C++:
protobuf_generate_cpp (
<srcs-var> <hdrs-var>
[DESCRIPTORS <var>]
[EXPORT_MACRO <macro>]
[<proto-file>...])
<srcs-var>Variable to define with autogenerated source files
<hdrs-var>Variable to define with autogenerated header files
DESCRIPTORS <var>Added in version 3.10: Variable to define with autogenerated descriptor files, if requested.
EXPORT_MACRO <macro>is a macro which should expand to __declspec(dllexport) or
__declspec(dllimport) depending on what is being compiled.
<proto-file>....proto files
Added in version 3.4.
Add custom commands to process .proto files to Python:
protobuf_generate_python (<py-srcs-var> [<proto-file>...])
<py-srcs-var>Variable to define with autogenerated Python files
<proto-file>....proto files
Added in version 3.13.
Automatically generate source files from .proto schema files at build time:
protobuf_generate (
TARGET <target>
[LANGUAGE <lang>]
[OUT_VAR <var>]
[EXPORT_MACRO <macro>]
[PROTOC_OUT_DIR <dir>]
[PLUGIN <plugin>]
[PLUGIN_OPTIONS <plugin-options>]
[DEPENDENCIES <dependencies>]
[PROTOS <proto-file>...]
[IMPORT_DIRS <dir>...]
[GENERATE_EXTENSIONS <extension>...]
[PROTOC_OPTIONS <option>...]
[PROTOC_EXE <executable>]
[APPEND_PATH])
APPEND_PATHA flag that causes the base path of all proto schema files to be added to
IMPORT_DIRS.
LANGUAGE <lang>A single value: cpp or python. Determines what kind of source files are being generated. Defaults to cpp.
OUT_VAR <var>Name of a CMake variable that will be filled with the paths to the generated source files.
EXPORT_MACRO <macro>Name of a macro that is applied to all generated Protobuf message classes and extern variables. It can, for example, be used to declare DLL exports.
PROTOC_OUT_DIR <dir>Output directory of generated source files. Defaults to CMAKE_CURRENT_BINARY_DIR.
PLUGIN <plugin>Added in version 3.21.
An optional plugin executable. This could, for example, be the path to
grpc_cpp_plugin.
PLUGIN_OPTIONS <plugin-options>Added in version 3.28.
Additional options provided to the plugin, such as generate_mock_code=true
for the gRPC cpp plugin.
DEPENDENCIES <dependencies>Added in version 3.28.
Arguments forwarded to the DEPENDS of the underlying add_custom_command
invocation.
TARGET <target>CMake target that will have the generated files added as sources.
PROTOS <proto-file>...List of proto schema files. If omitted, then every source file ending in proto of TARGET will be used.
IMPORT_DIRS <dir>...A common parent directory for the schema files. For example, if the schema file is
proto/helloworld/helloworld.proto and the import directory proto/ then the
generated files are ${PROTOC_OUT_DIR}/helloworld/helloworld.pb.h and
${PROTOC_OUT_DIR}/helloworld/helloworld.pb.cc.
GENERATE_EXTENSIONS <extension>...If LANGUAGE is omitted then this must be set to the extensions that protoc generates.
PROTOC_OPTIONS <option>...Added in version 3.28.
Additional arguments that are forwarded to protoc.
PROTOC_EXE <executable>Added in version 4.0.
Command name, path, or CMake executable used to generate protobuf bindings.
If omitted, protobuf::protoc is used.
Example:
find_package(gRPC CONFIG REQUIRED)
find_package(Protobuf REQUIRED)
add_library(ProtoTest Test.proto)
target_link_libraries(ProtoTest PUBLIC gRPC::grpc++)
protobuf_generate(TARGET ProtoTest)
protobuf_generate(
TARGET ProtoTest
LANGUAGE grpc
PLUGIN protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>
PLUGIN_OPTIONS generate_mock_code=true
GENERATE_EXTENSIONS .grpc.pb.h .grpc.pb.cc)