protobuf for C++: complete guide with examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What are Protocol Buffers (protobuf) and why are they needed?

\n\n

Protocol Buffers (protobuf) is a powerful, efficient, and extensible mechanism for serializing structured data, developed by Google. It allows you to define the data structure once in a special file (.proto), and then generate source code in various programming languages (C++, Java, Python, Go, etc.) for reading and writing this data.

\n\n

Why is protobuf needed?

\n
    \n
  • Efficiency: Protobuf serializes data into a binary format that is significantly more compact and faster to process than text formats such as XML or JSON. This is especially important for high-load systems and microservice architecture.
  • \n
  • Speed: Data serialization and deserialization are orders of magnitude faster than in traditional text formats.
  • \n
  • Type safety: The generated code provides strict typing, which reduces the number of runtime errors.
  • \n
  • Cross-platform and multi-language: You can define the data schema once and use it in projects in C++, Python, Java, and other languages.
  • \n
  • Backward compatibility: Protobuf supports data schema evolution: you can add new fields without breaking older versions of applications.
  • \n
\n\n

Installing protobuf for C++

\n\n

To work with protobuf in C++, you need to install two things: the protoc compiler (converts .proto files into C++ code) and the protobuf library itself for C++.

\n\n

1. Installing the protoc compiler

\n

The easiest way is to download a pre-built binary from the official protobuf releases page on GitHub.

\n
# Example for Linux (Ubuntu/Debian)\nsudo apt-get install protobuf-compiler\n\n# Example for macOS (via Homebrew)\nbrew install protobuf\n\n# For Windows, download protoc-x.x.x-win64.zip and add the path to protoc.exe to the PATH environment variable
\n\n

2. Installing the protobuf library for C++

\n

It is recommended to build the library from source or use a package manager.

\n
# Ubuntu/Debian\nsudo apt-get install libprotobuf-dev\n\n# macOS (Homebrew)\nbrew install protobuf\n\n# Building from source (universal method)\ngit clone https://github.com/protocolbuffers/protobuf.git\ncd protobuf\ngit submodule update --init --recursive\nmkdir build && cd build\ncmake -DCMAKE_BUILD_TYPE=Release -Dprotobuf_BUILD_TESTS=OFF ..\nmake -j$(nproc)\nsudo make install
\n\n

3. Configuring a CMake project (recommended method)

\n

Add to your CMakeLists.txt:

\n
find_package(Protobuf REQUIRED)\ninclude_directories(${Protobuf_INCLUDE_DIRS})\n\n# Automatic compilation of .proto files\nfile(GLOB PROTO_FILES "*.proto")\nprotobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${PROTO_FILES})\n\nadd_executable(my_program main.cpp ${PROTO_SRCS})\ntarget_link_libraries(my_program ${Protobuf_LIBRARIES})
\n\n

Quick start with a code example

\n\n

Let's create a simple example to demonstrate the basics. We'll define a data structure for a contact.

\n\n

Step 1: Create a .proto file

\n

File contact.proto:

\n
syntax = "proto3";\n\npackage tutorial;\n\nmessage Person {\n  string name = 1;\n  int32 age = 2;\n  string email = 3;\n}
\n\n

Step 2: Generate C++ code

\n
protoc --cpp_out=. contact.proto
\n

After executing the command, the files contact.pb.h and contact.pb.cc will appear.

\n\n

Step 3: Write C++ code

\n

File main.cpp:

\n
#include <iostream>\n#include <fstream>\n#include "contact.pb.h"\n\nusing namespace std;\n\nint main() {\n    // Make sure the library is initialized\n    GOOGLE_PROTOBUF_VER                    

Recommendations