2020年5月8日 星期五

Google Protocol Buffer Windows for Python and NodeJS

https://zhuanlan.zhihu.com/p/38601419

Environment

Windows 10
Miniconda 3.7
Google Protocol Buffer v3.11.4

Install
download https://github.com/protocolbuffers/protobuf/releases/download/v3.11.4/protoc-3.11.4-win64.zip
copy to C:\Windows\System32\protoc.exe
run pip install protobuf

Create addressbook.proto

syntax = "proto2";

package tutorial;

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    required string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phones = 4;
}

message AddressBook {
  repeated Person people = 1;
}

1. Python

Install

pip install protobuf

Otherwise it causes ModuleNotFoundError: No module named 'google'

Create compile.bat

protoc.exe -I=./ --python_out=./ addressbook.proto
PAUSE


This compiles from addressbook.proto to addressbook_pb2.py

Create main.py

import addressbook_pb2 as addressbook
source = addressbook.Person()
source.name = 'Alice'
source.id = 123456
serialized = source.SerializeToString()
print(len(serialized), serialized)
parsed = addressbook.Person()
parsed.ParseFromString(serialized)
print(parsed.name)
print(parsed.id)

11 b'\n\x05Alice\x10\xc0\xc4\x07'
Alice
123456

2. NodeJS

Install

npm i protobufjs

Create compile.bat

protoc.exe -I=./ --python_out=./ addressbook.proto
PAUSE


Create main.js

TBC

3. C#

C# is also quite simple

Install-Package Google.Protobuf -Version 3.11.4

protoc.exe -I=./ --csharp_out=./ addressbook.proto

4. C++

C+ setup is much harder than Python and C#

follow here.
https://github.com/protocolbuffers/protobuf/blob/master/cmake/README.md

Key points:
cmake.exe
cl.exe
git.exe
download protobuf-cpp-3.11.4.zip from latest
protobuf>cd cmake
> mkdir build
> cd build
mkdir release & cd release
cmake -G "NMake Makefiles" ^
 -DCMAKE_BUILD_TYPE=Release ^
 -DCMAKE_INSTALL_PREFIX=../../../../install ^
 ../..
mkdir solution & cd solution
cmake -G "Visual Studio 15 2017" ^
 -DCMAKE_INSTALL_PREFIX=../../../../install ^
 ../..
cmake\build\release>nmake
cmake\build\release>nmake check
release>nmake install
Finally find a "install" folder nearby "protobuf" folder
Additional include"include" folder

#ifdef _DEBUG
#pragma comment(lib, "libprotobufd.lib")
#else
#pragma comment(lib, "libprotobuf.lib")
#endif

Debug: Change /MDd to /MTd
Release: Change/MD to /MT








End

2023 Promox on Morefine N6000 16GB 512GB

2023 Promox on Morefine N6000 16GB 512GB Software Etcher 100MB (not but can be rufus-4.3.exe 1.4MB) Proxmox VE 7.4 ISO Installer (1st ISO re...