ProtoBuffer 簡單例子

2021-09-22 13:44:57 字數 1101 閱讀 5936

最近學了一下protobuf,寫了乙個簡單的例子,如下:

person.proto檔案

[cpp]view plain

copy

message person  

message phone  

repeated string phonenum = 4;  //對應於cpp的vector  

}  

安裝好protoc以後,執行protoc person.proto --cpp_out=. 生成 person.pb.h和person.pb.cpp

寫檔案(write_person.cpp):

[cpp]view plain

copy

#include 

#include "person.pb.h"  

#include 

#include 

using namespace std;  

int main()  

編譯時要把生成的cpp和原始檔一起編譯,如下:g++ write_person.cpp person.pb.cpp -o write_person -i your_proto_include_path -l your_proto_lib_path -lprotoc -lprotobuf執行時記得要加上ld_library_path=your_proto_lib_path

讀檔案(read_person.cpp):

[cpp]view plain

copy

#include 

#include "person.pb.h"  

#include 

#include 

using namespace std;  

int main()  

編譯執行方法同上:g++ read_person.cpp person.pb.cpp -o read_person -i your_proto_include_path -l your_proto_lib_path -lprotoc -lprotobuf

protobuffer 簡單例子

php與protoBuffer入門例子

如下 message tree 編譯 如下 require once parser pb parser.php test new pbparser test parse test my.proto var dump file parsing done 儲存資料和讀取資料 如下 first inclu...

php與protoBuffer入門例子

1,先建立乙個test my.proto 檔案 proto 定義資料物件型別 如下 message tree 2,新建乙個php執行編譯上面的資料物件,使之生成pb proto test my.php,裡面生成上方的tree乙個類資訊 編譯 如下 require once parser pb par...

C 使用ProtoBuffer 心得

protobuffer比xml,json 好就不說了。關鍵就說兩個地方 為啥需要說著兩個呢,因為他的序列化後在 最前面需要加上長度,而這個並不是真正的長度,是經過乙個演算法生成的長度。1.把本地的字串轉換成 protobuffer ocommonmsg.set type duolabo common...