linux下安装protobuf教程+示例(详细)
2016-01-31 17:22
633 查看
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
5> 写消息文件:msg.proto
#include "msg.pb.h"
#include <fstream>
#include <iostream>
using namespace std;
int main(void)
{
lm::helloworld msg1;
msg1.set_id(101);
msg1.set_str("hello");
fstream output("./log", ios::out | ios::trunc | ios::binary);
if (!msg1.SerializeToOstream(&output)) {
cerr << "Failed to write msg." << endl;
return -1;
}
return 0;
}
编译 write.cc
g++ msg.pb.cc write.cc -o write `pkg-config --cflags --libs protobuf` -lpthread
执行write
./write, 可以看到生成了log文件
7> 写反序列化的进程
reader.cc
all: write reader
clean:
rm -f write reader msg.*.cc msg.*.h *.o log
proto_msg:
protoc --cpp_out=. msg.proto
write: msg.pb.cc write.cc
g++ msg.pb.cc write.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`
http://www.cppblog.com/colorful/archive/2012/05/05/173761.html
安装步骤如下所示:
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
5> 写消息文件:msg.proto
#include "msg.pb.h"
#include <fstream>
#include <iostream>
using namespace std;
int main(void)
{
lm::helloworld msg1;
msg1.set_id(101);
msg1.set_str("hello");
fstream output("./log", ios::out | ios::trunc | ios::binary);
if (!msg1.SerializeToOstream(&output)) {
cerr << "Failed to write msg." << endl;
return -1;
}
return 0;
}
编译 write.cc
g++ msg.pb.cc write.cc -o write `pkg-config --cflags --libs protobuf` -lpthread
执行write
./write, 可以看到生成了log文件
7> 写反序列化的进程
reader.cc
all: write reader
clean:
rm -f write reader msg.*.cc msg.*.h *.o log
proto_msg:
protoc --cpp_out=. msg.proto
write: msg.pb.cc write.cc
g++ msg.pb.cc write.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`
http://www.cppblog.com/colorful/archive/2012/05/05/173761.html
相关文章推荐
- Linux下Mysql简单操作
- CentOS6.5下Git服务器搭建
- linux下删除指定文件之外的其他文…
- CentOS Linux安装Atomic源(yu…
- linux下vi/vim 中文汉字乱码解决
- centos关机与重启命令详解
- linux下rar包的压缩与解压方案(转)
- centos的软件安装方法rpm和yum
- CentOS 6.2 yum安装配置…
- Linux 图形界面与命令行模式…
- 实例浅关-自增自减
- CentOS 6.5 安装OSA监控精灵监控主机
- linux下常用的几个时间函数:time,gettimeofday,clock_gettime,_ftime
- linux中的阻塞机制及等待队列
- Linux学习:第九周
- Linux学习笔记(4)----连接文件 ln
- centos6.5 安装 xtables-addons 过程及报错解决
- linux之链表和哈希表应用浅析一
- Linux操作系统Swapping(交换)
- Linux进程管理