1 安裝protoc
2 把protoc.exe增加到環境變量path,這樣方便運行protoc
3 書寫proto文件。eg:test.proto
syntax = "proto2";
message Book
{
optional string name = 1;
optional int32 pages = 2;
optional float price = 3;
}
message Student
{
optional int32 age = 1;
optional string name = 2;
optional float score = 3;
repeated Book arrBook = 4;
}
4 生成C++代碼
protoc -I=Proto文件路徑 –cpp_out=指定輸出.h和.cc的目錄 Proto文件,也可以使用protoc -h 查看更多幫助。
格式:protoc -I = proto文件路徑 --cpp_out = 輸出文件路徑 proto文件名
生成結果:
此時可以直接拷貝到工程裏面使用了。