linux下Google的Protobuf安裝及使用筆記


protobuf

項目主頁:http://code.google.com/p/protobuf/

下載:http://code.google.com/p/protobuf/downloads/list protobuf-2.4.1.tar.gz

解壓后進入protobuf-2.4.1目錄進行安裝:

1、./configure(注:默認可能會安裝在/usr/local目錄下,可以加--prefix=/usr來指定安裝到/usr/lib下,可以免去路徑的設置,路徑設置見Linux命令pkg-config

./configure --prefix=/usr/local/protobuf

2、make

3、make check

4、make install(需要超級用戶root權限)

二、使用

1、寫proto文件,定義消息具體格式。如:helloworld.proto

package lm;

message helloworld

{

required int32 id = 1;//ID

required string str = 2;//str

optional int32 opt = 3;//optional field

}

2、使用protoc來編譯生成對應語言的文件

--cpp_out:表示輸出c++語言使用格式,--java_out,--python_out等,其他第三方的插件:Third-Party Add-ons for Protocol Buffers

此時會生成helloworld.pb.h及helloworld.pb.cc兩個文件

3、Write A message

View Code

4、Read A message

View Code

5、編譯及運行

g++ -g -o Writer helloworld.pb.cc writermessage.cpp `pkg-config --cflags --libs protobuf`

g++ -g -o Reader helloworld.pb.cc Readermessage.cpp `pkg-config --cflags --libs protobuf`


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM