Use Protocol Buffers with C++ — installation, code generation, serialization, and arena allocation.
The C++ Protocol Buffers runtime (version 7.35-dev) provides generated classes for each message type, along with utilities for serialization, parsing, and arena allocation.
Add a dependency on the protobuf Bazel target from the protobuf repository, or link against libprotobuf when using CMake. See cmake/README.md for CMake setup.
Due to ABI instability in C++, linking against different versions of libprotobuf in the same binary will cause crashes. Ensure the version of protoc used to generate code matches the runtime library version you link against.
Arena allocation improves performance by reducing heap fragmentation. All messages allocated on an arena are freed when the arena is destroyed:
#include <google/protobuf/arena.h>#include "addressbook.pb.h"google::protobuf::Arena arena;tutorial::AddressBook* address_book = google::protobuf::Arena::Create<tutorial::AddressBook>(&arena);tutorial::Person* person = address_book->add_people();person->set_name("Arena User");person->set_id(1);// No need to delete address_book — freed when arena goes out of scope
Arena allocation is most beneficial for workloads that create and destroy large numbers of messages. Use arena.SpaceUsed() and arena.SpaceAllocated() to profile memory usage.
google::protobuf::Message — base class for all generated messages. Provides SerializeToString, ParseFromString, ByteSizeLong, DebugString, and MergeFrom.
google::protobuf::Arena — arena allocator. Use Arena::Create<T>(&arena) to allocate messages on the arena.
Utilities
google::protobuf::util::TimeUtil — helpers for Timestamp and Duration well-known types.
google::protobuf::ShutdownProtobufLibrary() — release global objects on exit.
The C++ runtime does not guarantee ABI compatibility across versions. If you update libprotobuf, recompile all code that depends on it. Consider using linkstatic = True in Bazel cc_binary rules to avoid runtime version mismatches.