打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Google Protobuf的安装、配置、以及简单demo编译
【准备工作】

【前提
我是以root用户的身份来登录的,非root用户可以su命令登录root账号,或者在需要权限的命令前面加sudo。

【安装】
假设下载的是protobuf-2.1.0.tar.gz
     
tar -zxvf protobuf-2.1.0.tar.gz 
cd protobuf-2.1.0 
./configure --prefix=/opt/protobuf    (这里指定的路径可以是任意)
make
make check 
make install

【配置】
1、环境变量(方法可以有很多种,这里我修改的是/etc/profile)
vim /etc/profile
    
加入以下部分
PROTOBUF_HOME=/opt/protobuf
PROTOBUF_PKG_CONFIG_PATH=${PROTOBUF_HOME}/lib/pkgconfig

export data-path="${PATH}:${PROTOBUF_HOME}/bin:"
export PKG_CONFIG_data-path="${PKG_CONFIG_PATH}:${PROTOBUF_PKG_CONFIG_PATH}"

在~/.profile中添加上面两行export代码,否则上面两行export不会生效。
    
2、动态链接库路径
vim /etc/ld.so.conf
添加这行
/opt/protobuf/lib
    
为了让动态链接库修改生效
ldconfig   (ldconfig命令的作用见    http://www.xxlinux.com/linux/article/accidence/technique/20081230/14754.html )

【简单的demo编译
1、写pb文件(消息文件)
msg.proto

package test;   
message msg   
{   
    required int32     id = 1;     
    required string    str = 2;   
    optional int32     opt = 3;  
}
   
2、pb文件转换成cpp文件
protoc -I=. --cpp_out=. msg.proto  (java或者python的话,第二个参数不一样,这里是针对cpp)
生成了msg.pb.h 和msg.pb.cc

3、写序列化消息的进程
writer.cc

#include "msg.pb.h"  
#include <fstream>  
#include <iostream>  

using namespace std;  
  
int main(void)   
{   
    test::msg obj;   
    obj.set_id(101);   
    obj.set_str("hello");   
    fstream output("./log", ios::out | ios::trunc | ios::binary);   
  
    if (!obj.SerializeToOstream(&output)) {   
        cerr << "Failed to write msg." << endl;   
        return -1;   
    }          
    return 0;   
}  

编译 writer.cc 
g++  msg.pb.cc writer.cc -o writer  `pkg-config --cflags --libs protobuf` -lpthread

./writer   ,   会在本地生成log文件

4、写反序列化的进程
reader.cc

#include "msg.pb.h"  
#include <fstream>  
#include <iostream>  

using namespace std;  
  
void PrintMsg(const test::msg & obj) {    
    cout << obj.id() << endl;   
    cout << obj.str() << endl;   
}   
  
int main(int argc, char* argv[]) {   
    test::msg obj;     
    {   
        fstream input("./log", ios::in | ios::binary);   
        if (!obj.ParseFromIstream(&input)) {   
            cerr << "Failed to parse address book." << endl;   
            return -1;   
        }         
    }    
    PrintMsg(obj);   
}  

编译
g++  msg.pb.cc reader.cc -o reader  `pkg-config --cflags --libs protobuf` -lpthread

./reader 
输出 :
101
hello

5、Makefile
all : writer reader  

clean :  
    rm -f writer reader msg.*.cc msg.*.h *.o  log  

proto_msg :  
    protoc --cpp_out=. msg.proto  

write : msg.pb.cc writer.cc  
    g++  msg.pb.cc writer.cc -o write  `pkg-config --cflags --libs protobuf`  

reader : msg.pb.cc reader.cc  
    g++  msg.pb.cc reader.cc -o reader  `pkg-config --cflags --libs protobuf` 
【结束


Allen Lin
2013/06/26 
    
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Unity配置Protobuf协议
Google Protobuf - 实现跨平台跨语言的序列化/反序列化
CentOS7下安装gRPC(C++环境搭建)
云风的 BLOG: 使用 luajit 的 ffi 绑定 zeromq
ToLua SimpleFramework NGUI/UGUI基础知识[4]
Google Protocol Buffers安装以及简单使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服