linux下安装protobuf教程+示例

1 在网站 http://code.google.com/p/protobuf/downloads/list上可以下载 Protobuf 的源代码。然后解压编译安装便可以使用它了。
安装步骤如下所示:
 tar -xzf protobuf-2.1.0.tar.gz 
 cd protobuf-2.1.0 
 ./configure --prefix=/usr/local/protobuf
 make 
 make check 
 make install 
 
 2 > sudo vim /etc/profile
添加
export PATH=$PATH:/usr/local/protobuf/bin/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/

保存执行
source /etc/profile

同时 在~/.profile中添加上面两行代码,否则会出现登录用户找不到protoc命令

3 > 配置动态链接库路径
sudo vim /etc/ld.so.conf
插入:
/usr/local/protobuf/lib

4 > su  #root 权限
ldconfig

  1. 开始写.proto文件:  
  2. BaseMessage.proto:  
  3. message MessageBase  
  4. {  
  5.     required int32 opcode = 1;  
  6.     // other: sendMgrId, sendId, recvMgrId, recvId, ...  
  7. }  
  8.   
  9. message BaseMessage  
  10. {  
  11.     required MessageBase msgbase = 1;  
  12. }  
  13.   
  14. BaseMessage.proto是其它消息proto文件的基础,以容器模块的C2S_GetContainerInfo为例:  
  15. ContainerMessage.proto:  
  16. import "BaseMessage.proto";  
  17.   
  18. message C2SGetContainerInfoMsg  
  19. {  
  20.     required MessageBase msgbase = 1;  
  21.     optional int32 containerType = 2;  
  22. }  
  23.   
  24. .proto文件编写规则:  
  25. 1)所有消息都需要包含msgbase这项,并编号都为1,即:  
  26.   required MessageBase msgbase = 1;  
  27. 2)除了msgbase这项写成required外,其它所有项都写成optional。  
  28.   
  29. 编译 .proto 文件  
  30. protoc -I=. --cpp_out=. BaseMessage.proto  
  31. protoc -I=. --cpp_out=. ContainerMessage.proto  
  32. 生成BaseMessage.pb.h、BaseMessage.pb.cc  
  33.     ContainerMessage.pb.h、ContainerMessage.pb.cc  
  34. 将它们添加到工程文件中。  
  35.   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值