找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 4210|回复: 0

linux下安装protobuf教程+示例(详细)

[复制链接]
发表于 2012-4-16 23:40:20 | 显示全部楼层 |阅读模式
1 在网站 http://code.google.com/p/protobuf/downloads/list上可以下载 Protobuf 的源代码。然后解压编译安装便可以使用它了。
安装步骤如下所示:
  1. tar -xzf protobuf-2.1.0.tar.gz
  2. cd protobuf-2.1.0
  3. ./configure --prefix=/usr/local/protobuf
  4. make
  5. make check
  6. make install
复制代码


2 > sudo vim /etc/profile
添加
  1. export PATH=$PATH:/usr/local/protobuf/bin/
  2. 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

5> 写消息文件:msg.proto
  1. package lm;
  2. message helloworld
  3. {
  4.     required int32     id = 1;  // ID   
  5.     required string    str = 2;  // str  
  6.     optional int32     opt = 3;  //optional field
  7. }
复制代码
将消息文件msg.proto映射成cpp文件
protoc -I=. --cpp_out=. msg.proto
可以看到生成了
msg.pb.h 和msg.pb.cc


6> 写序列化消息的进程
write.cc
  1. #include "msg.pb.h"
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace std;
  5. int main(void)
  6. {
  7.     lm::helloworld msg1;
  8.     msg1.set_id(101);
  9.     msg1.set_str("hello");
  10.     fstream output("./log", ios::out | ios::trunc | ios::binary);
  11.     if (!msg1.SerializeToOstream(&output)) {
  12.         cerr << "Failed to write msg." << endl;
  13.         return -1;
  14.     }        
  15.     return 0;
  16. }
复制代码
编译 write.cc
g++  msg.pb.cc write.cc -o write  `pkg-config --cflags --libs protobuf` -lpthread

执行write
./write, 可以看到生成了log文件


7> 写反序列化的进程
reader.cc
  1. #include "msg.pb.h"
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace std;
  5. void ListMsg(const lm::helloworld & msg) {  
  6.     cout << msg.id() << endl;
  7.     cout << msg.str() << endl;
  8. }
  9. int main(int argc, char* argv[]) {
  10.     lm::helloworld msg1;
  11.     {
  12.         fstream input("./log", ios::in | ios::binary);
  13.         if (!msg1.ParseFromIstream(&input)) {
  14.             cerr << "Failed to parse address book." << endl;
  15.             return -1;
  16.         }      
  17.     }
  18.     ListMsg(msg1);
  19. }
复制代码
编译:g++  msg.pb.cc reader.cc -o reader  `pkg-config --cflags --libs protobuf` -lpthread
执行./reader 输出 :
101
hello


8> 写Makefile文件
  1. all: write reader
  2. clean:
  3.     rm -f write reader msg.*.cc msg.*.h *.o  log
  4. proto_msg:
  5.     protoc --cpp_out=. msg.proto
  6. write: msg.pb.cc write.cc
  7.     g++  msg.pb.cc write.cc -o write  `pkg-config --cflags --libs protobuf`
  8. reader: msg.pb.cc reader.cc
  9.     g++  msg.pb.cc reader.cc -o reader  `pkg-config --cflags --libs protobuf`
复制代码


作者:realxie 发表于2012-4-12 23:33:25 原文链接
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-4 18:05 , Processed in 0.020197 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表