Original link: http://i.lckiss.com/?p=8088
Background: When I need to participate in pb file parsing at work, I mainly refer to the market-related configuration files here.
System: elementary OS 5.1.7 Hera (built on Ubuntu 18.04.6 LTS)
I don’t know if it is because Ubuntu is too old and the protobuf-compiler in the source is too old, or because the official update is too frequent and Ubuntu ppa is unwilling to synchronize, record a simple uninstall and installation.
If the protobuf version of the store happens to be the version you need, there is no need to toss ( sudo apt install protobuf-compiler
). Otherwise~
Source file download
The official release packages are hosted on GitHub: https://github.com/protocolbuffers/protobuf/releases
Select the source package of the corresponding architecture as needed, I choose here: protobuf-all-21.5.tar.gz
compile/install
Unzip the above source package, cd to the unzip directory, and execute:
./configure
Then the usual routine for compilation:
make && make install
PS: Depending on each system environment, make install may require sudo escalation.
No accident, the installation is complete, you can look at the version:
protoc --version
uninstall
Execute in the source directory (depending on the permissions at the time of installation, sudo may also be required):
make uninstall
usage
As for how to use this thing~ It depends on the requirements of the demander. Python is converted to Python script, Java is converted to Java code, and the pb file can only be used after the conversion. Because it is a data protocol itself:
Protocol Buffers (ProtoBuf for short) is an open source cross-platform serialized data structure protocol. It is useful for programs that store data or communicate over a network. This method contains an interface description language that describes some data structures, and provides programming tools based on these descriptions to generate code that will be used to generate or parse byte streams representing those data structures.
E.g:
Switch to Python:
protoc --python_out=./pb/ --proto_path=./pb/ ./pb/*.proto
Convert to Java:
protoc --java_out=./pb/ --proto_path=./pb/ ./pb/*.proto
other
The extra files are the files we need to use. As for the case where the Java files are too large, we need to do subcontracting. References to this part:
https://developers.google.com/protocol-buffers/docs/reference/java-generated
The translation is (translation):
--java_out=
Produces Java output when the protocol buffer compiler is invoked with the command line flag. The argument to the --java_out=
option is the directory where you want the compiler to write Java output. For each .proto
file input, the compiler creates a wrapper .java
file that contains a Java class that represents the .proto
file itself.
If the .proto
file contains a line like:
option java_multiple_files = true;
.java
The compiler then also creates a separate .proto
file for each top-level message, enumeration and service declared in the file.
Otherwise (ie when the option is false; this is the default), the above wrapper classes are also used as outer classes and generated classes for each top-level message, enum and service declared in the file java_multiple_files
/ .proto
are nested in in the outer wrapper class.
Raise chestnuts:
syntax = "proto3"; package HqPcDef; //Add file header here option java_multiple_files = true; ...
above;
refer to:
https://developers.google.com/protocol-buffers/docs/reference/overview
https://en.m.wikipedia.org/en-hans/Protocol_Buffers
This article is reprinted from: http://i.lckiss.com/?p=8088
This site is for inclusion only, and the copyright belongs to the original author.