您的位置:首页 > 运维架构 > Linux

linux下Google的Protobuf安装及使用笔记

2012-12-04 11:37 726 查看
点击打开链接

protobuf

项目主页:http://code.google.com/p/protobuf/

下载:  http://protobuf.googlecode.com/files/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权限)

二、使用

关于protobuf的使用,可以参考这里

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`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: