由于现在protobuf越来越复杂了,自己去编译,还是比较麻烦。
比如最新的V28版本,就会要求使用cmake或者bazel来编译了。
如果不要求使用最新的版本,直接使用系统里带的版本也是可以的。
可以进行如下操作:
sudo apt install protobuf-compiler libprotobuf-dev -y
这样就安装成功所有开发相关的库文件了,这里主要是针对C++的。
protoc --version
运行上面的命令,就可以输出对应的版本:
可以看到我这里的系统带的是3.21.12的版本,对于我来说,这样是满足了需求了。不去折腾最新的版本。
syntax = "proto3";
message Person
{
string name = 1;
int32 age = 18;
}
上面的文件,就可以使用命令行进行编译:
protoc --cpp_out=./ Test.proto
输出两个文件:
然后就可以在树莓派里使用它来编译了。
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: Test.proto
#ifndef GOOGLE_PROTOBUF_INCLUDED_Test_2eproto
#define GOOGLE_PROTOBUF_INCLUDED_Test_2eproto
#include <limits>
#include <string>
#include <google/protobuf/port_def.inc>
#if PROTOBUF_VERSION < 3021000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers. Please update
#error your headers.
#endif
#if 3021012 < PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers. Please
#error regenerate this file with a newer version of protoc.
#endif
#include <google/protobuf/port_undef.inc>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/arena.h>
#include <google/protobuf/arenastring.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/metadata_lite.h>
#include <google/protobuf/generated_message_reflection.h>
#include <google/protobuf/message.h>
#include <google/protobuf/repeated_field.h> // IWYU pragma: export
#include <google/protobuf/extension_set.h> // IWYU pragma: export
#include <google/protobuf/unknown_field_set.h>
// @@protoc_insertion_point(includes)
#include <google/protobuf/port_def.inc>
#define PROTOBUF_INTERNAL_EXPORT_Test_2eproto
PROTOBUF_NAMESPACE_OPEN
namespace internal {
class AnyMetadata;
} // namespace internal
PROTOBUF_NAMESPACE_CLOSE
// Internal implementation detail -- do not use these members.
struct TableStruct_Test_2eproto {
static const uint32_t offsets[];
};
extern const ::PROTOBUF_NAMESPACE_ID::internal::DescriptorTable descriptor_table_Test_2eproto;
class Person;
struct PersonDefaultTypeInternal;
extern PersonDefaultTypeInternal _Person_default_instance_;
PROTOBUF_NAMESPACE_OPEN
template<> ::Person* Arena::CreateMaybeMessage<::Person>(Arena*);
PROTOBUF_NAMESPACE_CLOSE
// ===================================================================
class Person final :
public ::PROTOBUF_NAMESPACE_ID::Message /* @@protoc_insertion_point(class_definition:Person) */ {
public:
inline Person() : Person(nullptr) {}
~Person() override;
explicit PROTOBUF_CONSTEXPR Person(::PROTOBUF_NAMESPACE_ID::internal::ConstantInitialized);
Person(const Person& from);
Person(Person&& from) noexcept
: Person() {
*this = ::std::move(from);
}
inline Person& operator=(const Person& from) {
CopyFrom(from);
return *this;
}
inline Person& operator=(Person&& from) noexcept {
if (this == &from) return *this;
if (GetOwningArena() == from.GetOwningArena()
#ifdef PROTOBUF_FORCE_COPY_IN_MOVE
&& GetOwningArena() != nullptr
#endif // !PROTOBUF_FORCE_COPY_IN_MOVE
) {
InternalSwap(&from);
} else {
CopyFrom(from);
}
return *this;
}
static const ::PROTOBUF_NAMESPACE_ID::Descriptor* descriptor() {
return GetDescriptor();
}
static const ::PROTOBUF_NAMESPACE_ID::Descriptor* GetDescriptor() {
return default_instance().GetMetadata().descriptor;
}
static const ::PROTOBUF_NAMESPACE_ID::Reflection* GetReflection() {
return default_instance().GetMetadata().reflection;
}
static const Person& default_instance() {
return *internal_default_instance();
}
static inline const Person* internal_default_instance() {
return reinterpret_cast<const Person*>(
&_Person_default_instance_);
}
static constexpr int kIndexInFileMessages =
0;
friend void swap(Person& a, Person& b) {
a.Swap(&b);
}
inline void Swap(Person* other) {
if (other == this) return;
#ifdef PROTOBUF_FORCE_COPY_IN_SWAP
if (GetOwningArena() != nullptr &&
GetOwningArena() == other->GetOwningArena()) {
#else // PROTOBUF_FORCE_COPY_IN_SWAP
if (GetOwningArena() == other->GetOwningArena()) {
#endif // !PROTOBUF_FORCE_COPY_IN_SWAP
InternalSwap(other);
} else {
::PROTOBUF_NAMESPACE_ID::internal::GenericSwap(this, other);
}
}
void UnsafeArenaSwap(Person* other) {
if (other == this) return;
GOOGLE_DCHECK(GetOwningArena() == other->GetOwningArena());
InternalSwap(other);
}
// implements Message ----------------------------------------------
Person* New(::PROTOBUF_NAMESPACE_ID::Arena* arena = nullptr) const final {
return CreateMaybeMessage<Person>(arena);
}
using ::PROTOBUF_NAMESPACE_ID::Message::CopyFrom;
void CopyFrom(const Person& from);
using ::PROTOBUF_NAMESPACE_ID::Message::MergeFrom;
void MergeFrom( const Person& from) {
Person::MergeImpl(*this, from);
}
private:
static void MergeImpl(::PROTOBUF_NAMESPACE_ID::Message& to_msg, const ::PROTOBUF_NAMESPACE_ID::Message& from_msg);
public:
PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear() final;
bool IsInitialized() const final;
size_t ByteSizeLong() const final;
const char* _InternalParse(const char* ptr, ::PROTOBUF_NAMESPACE_ID::internal::ParseContext* ctx) final;
uint8_t* _InternalSerialize(
uint8_t* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const final;
int GetCachedSize() const final { return _impl_._cached_size_.Get(); }
private:
void SharedCtor(::PROTOBUF_NAMESPACE_ID::Arena* arena, bool is_message_owned);
void SharedDtor();
void SetCachedSize(int size) const final;
void InternalSwap(Person* other);
private:
friend class ::PROTOBUF_NAMESPACE_ID::internal::AnyMetadata;
static ::PROTOBUF_NAMESPACE_ID::StringPiece FullMessageName() {
return "Person";
}
protected:
explicit Person(::PROTOBUF_NAMESPACE_ID::Arena* arena,
bool is_message_owned = false);
public:
static const ClassData _class_data_;
const ::PROTOBUF_NAMESPACE_ID::Message::ClassData*GetClassData() const final;
::PROTOBUF_NAMESPACE_ID::Metadata GetMetadata() const final;
// nested types ----------------------------------------------------
// accessors -------------------------------------------------------
enum : int {
kNameFieldNumber = 1,
kAgeFieldNumber = 18,
};
// string name = 1;
void clear_name();
const std::string& name() const;
template <typename ArgT0 = const std::string&, typename... ArgT>
void set_name(ArgT0&& arg0, ArgT... args);
std::string* mutable_name();
PROTOBUF_NODISCARD std::string* release_name();
void set_allocated_name(std::string* name);
private:
const std::string& _internal_name() const;
inline PROTOBUF_ALWAYS_INLINE void _internal_set_name(const std::string& value);
std::string* _internal_mutable_name();
public:
// int32 age = 18;
void clear_age();
int32_t age() const;
void set_age(int32_t value);
private:
int32_t _internal_age() const;
void _internal_set_age(int32_t value);
public:
// @@protoc_insertion_point(class_scope:Person)
private:
class _Internal;
template <typename T> friend class ::PROTOBUF_NAMESPACE_ID::Arena::InternalHelper;
typedef void InternalArenaConstructable_;
typedef void DestructorSkippable_;
struct Impl_ {
::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr name_;
int32_t age_;
mutable ::PROTOBUF_NAMESPACE_ID::internal::CachedSize _cached_size_;
};
union { Impl_ _impl_; };
friend struct ::TableStruct_Test_2eproto;
};
// ===================================================================
// ===================================================================
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif // __GNUC__
// Person
// string name = 1;
inline void Person::clear_name() {
_impl_.name_.ClearToEmpty();
}
inline const std::string& Person::name() const {
// @@protoc_insertion_point(field_get:Person.name)
return _internal_name();
}
template <typename ArgT0, typename... ArgT>
inline PROTOBUF_ALWAYS_INLINE
void Person::set_name(ArgT0&& arg0, ArgT... args) {
_impl_.name_.Set(static_cast<ArgT0 &&>(arg0), args..., GetArenaForAllocation());
// @@protoc_insertion_point(field_set:Person.name)
}
inline std::string* Person::mutable_name() {
std::string* _s = _internal_mutable_name();
// @@protoc_insertion_point(field_mutable:Person.name)
return _s;
}
inline const std::string& Person::_internal_name() const {
return _impl_.name_.Get();
}
inline void Person::_internal_set_name(const std::string& value) {
_impl_.name_.Set(value, GetArenaForAllocation());
}
inline std::string* Person::_internal_mutable_name() {
return _impl_.name_.Mutable(GetArenaForAllocation());
}
inline std::string* Person::release_name() {
// @@protoc_insertion_point(field_release:Person.name)
return _impl_.name_.Release();
}
inline void Person::set_allocated_name(std::string* name) {
if (name != nullptr) {
} else {
}
_impl_.name_.SetAllocated(name, GetArenaForAllocation());
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
if (_impl_.name_.IsDefault()) {
_impl_.name_.Set("", GetArenaForAllocation());
}
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
// @@protoc_insertion_point(field_set_allocated:Person.name)
}
// int32 age = 18;
inline void Person::clear_age() {
_impl_.age_ = 0;
}
inline int32_t Person::_internal_age() const {
return _impl_.age_;
}
inline int32_t Person::age() const {
// @@protoc_insertion_point(field_get:Person.age)
return _internal_age();
}
inline void Person::_internal_set_age(int32_t value) {
_impl_.age_ = value;
}
inline void Person::set_age(int32_t value) {
_internal_set_age(value);
// @@protoc_insertion_point(field_set:Person.age)
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif // __GNUC__
// @@protoc_insertion_point(namespace_scope)
// @@protoc_insertion_point(global_scope)
#include <google/protobuf/port_undef.inc>
#endif // GOOGLE_PROTOBUF_INCLUDED_GOOGLE_PROTOBUF_INCLUDED_Test_2eproto
cc文件:
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: Test.proto
#include "Test.pb.h"
#include <algorithm>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/extension_set.h>
#include <google/protobuf/wire_format_lite.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/generated_message_reflection.h>
#include <google/protobuf/reflection_ops.h>
#include <google/protobuf/wire_format.h>
// @@protoc_insertion_point(includes)
#include <google/protobuf/port_def.inc>
PROTOBUF_PRAGMA_INIT_SEG
namespace _pb = ::PROTOBUF_NAMESPACE_ID;
namespace _pbi = _pb::internal;
PROTOBUF_CONSTEXPR Person::Person(
::_pbi::ConstantInitialized): _impl_{
/*decltype(_impl_.name_)*/{&::_pbi::fixed_address_empty_string, ::_pbi::ConstantInitialized{}}
, /*decltype(_impl_.age_)*/0
, /*decltype(_impl_._cached_size_)*/{}} {}
struct PersonDefaultTypeInternal {
PROTOBUF_CONSTEXPR PersonDefaultTypeInternal()
: _instance(::_pbi::ConstantInitialized{}) {}
~PersonDefaultTypeInternal() {}
union {
Person _instance;
};
};
PROTOBUF_ATTRIBUTE_NO_DESTROY PROTOBUF_CONSTINIT PROTOBUF_ATTRIBUTE_INIT_PRIORITY1 PersonDefaultTypeInternal _Person_default_instance_;
static ::_pb::Metadata file_level_metadata_Test_2eproto[1];
static constexpr ::_pb::EnumDescriptor const** file_level_enum_descriptors_Test_2eproto = nullptr;
static constexpr ::_pb::ServiceDescriptor const** file_level_service_descriptors_Test_2eproto = nullptr;
const uint32_t TableStruct_Test_2eproto::offsets[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {
~0u, // no _has_bits_
PROTOBUF_FIELD_OFFSET(::Person, _internal_metadata_),
~0u, // no _extensions_
~0u, // no _oneof_case_
~0u, // no _weak_field_map_
~0u, // no _inlined_string_donated_
PROTOBUF_FIELD_OFFSET(::Person, _impl_.name_),
PROTOBUF_FIELD_OFFSET(::Person, _impl_.age_),
};
static const ::_pbi::MigrationSchema schemas[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) = {
{ 0, -1, -1, sizeof(::Person)},
};
static const ::_pb::Message* const file_default_instances[] = {
&::_Person_default_instance_._instance,
};
const char descriptor_table_protodef_Test_2eproto[] PROTOBUF_SECTION_VARIABLE(protodesc_cold) =
"\n\nTest.proto\"#\n\006Person\022\014\n\004name\030\001 \001(\t\022\013\n\003"
"age\030\022 \001(\005b\006proto3"
;
static ::_pbi::once_flag descriptor_table_Test_2eproto_once;
const ::_pbi::DescriptorTable descriptor_table_Test_2eproto = {
false, false, 57, descriptor_table_protodef_Test_2eproto,
"Test.proto",
&descriptor_table_Test_2eproto_once, nullptr, 0, 1,
schemas, file_default_instances, TableStruct_Test_2eproto::offsets,
file_level_metadata_Test_2eproto, file_level_enum_descriptors_Test_2eproto,
file_level_service_descriptors_Test_2eproto,
};
PROTOBUF_ATTRIBUTE_WEAK const ::_pbi::DescriptorTable* descriptor_table_Test_2eproto_getter() {
return &descriptor_table_Test_2eproto;
}
// Force running AddDescriptors() at dynamic initialization time.
PROTOBUF_ATTRIBUTE_INIT_PRIORITY2 static ::_pbi::AddDescriptorsRunner dynamic_init_dummy_Test_2eproto(&descriptor_table_Test_2eproto);
// ===================================================================
class Person::_Internal {
public:
};
Person::Person(::PROTOBUF_NAMESPACE_ID::Arena* arena,
bool is_message_owned)
: ::PROTOBUF_NAMESPACE_ID::Message(arena, is_message_owned) {
SharedCtor(arena, is_message_owned);
// @@protoc_insertion_point(arena_constructor:Person)
}
Person::Person(const Person& from)
: ::PROTOBUF_NAMESPACE_ID::Message() {
Person* const _this = this; (void)_this;
new (&_impl_) Impl_{
decltype(_impl_.name_){}
, decltype(_impl_.age_){}
, /*decltype(_impl_._cached_size_)*/{}};
_internal_metadata_.MergeFrom<::PROTOBUF_NAMESPACE_ID::UnknownFieldSet>(from._internal_metadata_);
_impl_.name_.InitDefault();
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
_impl_.name_.Set("", GetArenaForAllocation());
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
if (!from._internal_name().empty()) {
_this->_impl_.name_.Set(from._internal_name(),
_this->GetArenaForAllocation());
}
_this->_impl_.age_ = from._impl_.age_;
// @@protoc_insertion_point(copy_constructor:Person)
}
inline void Person::SharedCtor(
::_pb::Arena* arena, bool is_message_owned) {
(void)arena;
(void)is_message_owned;
new (&_impl_) Impl_{
decltype(_impl_.name_){}
, decltype(_impl_.age_){0}
, /*decltype(_impl_._cached_size_)*/{}
};
_impl_.name_.InitDefault();
#ifdef PROTOBUF_FORCE_COPY_DEFAULT_STRING
_impl_.name_.Set("", GetArenaForAllocation());
#endif // PROTOBUF_FORCE_COPY_DEFAULT_STRING
}
Person::~Person() {
// @@protoc_insertion_point(destructor:Person)
if (auto *arena = _internal_metadata_.DeleteReturnArena<::PROTOBUF_NAMESPACE_ID::UnknownFieldSet>()) {
(void)arena;
return;
}
SharedDtor();
}
inline void Person::SharedDtor() {
GOOGLE_DCHECK(GetArenaForAllocation() == nullptr);
_impl_.name_.Destroy();
}
void Person::SetCachedSize(int size) const {
_impl_._cached_size_.Set(size);
}
void Person::Clear() {
// @@protoc_insertion_point(message_clear_start:Person)
uint32_t cached_has_bits = 0;
// Prevent compiler warnings about cached_has_bits being unused
(void) cached_has_bits;
_impl_.name_.ClearToEmpty();
_impl_.age_ = 0;
_internal_metadata_.Clear<::PROTOBUF_NAMESPACE_ID::UnknownFieldSet>();
}
const char* Person::_InternalParse(const char* ptr, ::_pbi::ParseContext* ctx) {
#define CHK_(x) if (PROTOBUF_PREDICT_FALSE(!(x))) goto failure
while (!ctx->Done(&ptr)) {
uint32_t tag;
ptr = ::_pbi::ReadTag(ptr, &tag);
switch (tag >> 3) {
// string name = 1;
case 1:
if (PROTOBUF_PREDICT_TRUE(static_cast<uint8_t>(tag) == 10)) {
auto str = _internal_mutable_name();
ptr = ::_pbi::InlineGreedyStringParser(str, ptr, ctx);
CHK_(ptr);
CHK_(::_pbi::VerifyUTF8(str, "Person.name"));
} else
goto handle_unusual;
continue;
// int32 age = 18;
case 18:
if (PROTOBUF_PREDICT_TRUE(static_cast<uint8_t>(tag) == 144)) {
_impl_.age_ = ::PROTOBUF_NAMESPACE_ID::internal::ReadVarint32(&ptr);
CHK_(ptr);
} else
goto handle_unusual;
continue;
default:
goto handle_unusual;
} // switch
handle_unusual:
if ((tag == 0) || ((tag & 7) == 4)) {
CHK_(ptr);
ctx->SetLastTag(tag);
goto message_done;
}
ptr = UnknownFieldParse(
tag,
_internal_metadata_.mutable_unknown_fields<::PROTOBUF_NAMESPACE_ID::UnknownFieldSet>(),
ptr, ctx);
CHK_(ptr != nullptr);
} // while
message_done:
return ptr;
failure:
ptr = nullptr;
goto message_done;
#undef CHK_
}
uint8_t* Person::_InternalSerialize(
uint8_t* target, ::PROTOBUF_NAMESPACE_ID::io::EpsCopyOutputStream* stream) const {
// @@protoc_insertion_point(serialize_to_array_start:Person)
uint32_t cached_has_bits = 0;
(void) cached_has_bits;
// string name = 1;
if (!this->_internal_name().empty()) {
::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::VerifyUtf8String(
this->_internal_name().data(), static_cast<int>(this->_internal_name().length()),
::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::SERIALIZE,
"Person.name");
target = stream->WriteStringMaybeAliased(
1, this->_internal_name(), target);
}
// int32 age = 18;
if (this->_internal_age() != 0) {
target = stream->EnsureSpace(target);
target = ::_pbi::WireFormatLite::WriteInt32ToArray(18, this->_internal_age(), target);
}
if (PROTOBUF_PREDICT_FALSE(_internal_metadata_.have_unknown_fields())) {
target = ::_pbi::WireFormat::InternalSerializeUnknownFieldsToArray(
_internal_metadata_.unknown_fields<::PROTOBUF_NAMESPACE_ID::UnknownFieldSet>(::PROTOBUF_NAMESPACE_ID::UnknownFieldSet::default_instance), target, stream);
}
// @@protoc_insertion_point(serialize_to_array_end:Person)
return target;
}
size_t Person::ByteSizeLong() const {
// @@protoc_insertion_point(message_byte_size_start:Person)
size_t total_size = 0;
uint32_t cached_has_bits = 0;
// Prevent compiler warnings about cached_has_bits being unused
(void) cached_has_bits;
// string name = 1;
if (!this->_internal_name().empty()) {
total_size += 1 +
::PROTOBUF_NAMESPACE_ID::internal::WireFormatLite::StringSize(
this->_internal_name());
}
// int32 age = 18;
if (this->_internal_age() != 0) {
total_size += 2 +
::_pbi::WireFormatLite::Int32Size(
this->_internal_age());
}
return MaybeComputeUnknownFieldsSize(total_size, &_impl_._cached_size_);
}
const ::PROTOBUF_NAMESPACE_ID::Message::ClassData Person::_class_data_ = {
::PROTOBUF_NAMESPACE_ID::Message::CopyWithSourceCheck,
Person::MergeImpl
};
const ::PROTOBUF_NAMESPACE_ID::Message::ClassData*Person::GetClassData() const { return &_class_data_; }
void Person::MergeImpl(::PROTOBUF_NAMESPACE_ID::Message& to_msg, const ::PROTOBUF_NAMESPACE_ID::Message& from_msg) {
auto* const _this = static_cast<Person*>(&to_msg);
auto& from = static_cast<const Person&>(from_msg);
// @@protoc_insertion_point(class_specific_merge_from_start:Person)
GOOGLE_DCHECK_NE(&from, _this);
uint32_t cached_has_bits = 0;
(void) cached_has_bits;
if (!from._internal_name().empty()) {
_this->_internal_set_name(from._internal_name());
}
if (from._internal_age() != 0) {
_this->_internal_set_age(from._internal_age());
}
_this->_internal_metadata_.MergeFrom<::PROTOBUF_NAMESPACE_ID::UnknownFieldSet>(from._internal_metadata_);
}
void Person::CopyFrom(const Person& from) {
// @@protoc_insertion_point(class_specific_copy_from_start:Person)
if (&from == this) return;
Clear();
MergeFrom(from);
}
bool Person::IsInitialized() const {
return true;
}
void Person::InternalSwap(Person* other) {
using std::swap;
auto* lhs_arena = GetArenaForAllocation();
auto* rhs_arena = other->GetArenaForAllocation();
_internal_metadata_.InternalSwap(&other->_internal_metadata_);
::PROTOBUF_NAMESPACE_ID::internal::ArenaStringPtr::InternalSwap(
&_impl_.name_, lhs_arena,
&other->_impl_.name_, rhs_arena
);
swap(_impl_.age_, other->_impl_.age_);
}
::PROTOBUF_NAMESPACE_ID::Metadata Person::GetMetadata() const {
return ::_pbi::AssignDescriptors(
&descriptor_table_Test_2eproto_getter, &descriptor_table_Test_2eproto_once,
file_level_metadata_Test_2eproto[0]);
}
// @@protoc_insertion_point(namespace_scope)
PROTOBUF_NAMESPACE_OPEN
template<> PROTOBUF_NOINLINE ::Person*
Arena::CreateMaybeMessage< ::Person >(Arena* arena) {
return Arena::CreateMessageInternal< ::Person >(arena);
}
PROTOBUF_NAMESPACE_CLOSE
// @@protoc_insertion_point(global_scope)
#include <google/protobuf/port_undef.inc>