protobuf简介
protobuf(Google Protocol Buffers)是Google提供一个具有高效的协议数据交换格式工具库(类似Json),但相比于Json,Protobuf有更高的转化效率,时间效率和空间效率都是JSON的3-5倍。
参考:
Protobuf的简单介绍、使用和分析
全方位评测:Protobuf性能到底有没有比JSON快5倍?
json与protobuf的速度之争
深入 ProtoBuf - 简介
海思平台交叉编译
参考:protobuf2.6.1海思平台交叉编译
准备:
sdk安装包:protobuf-all-3.5.1.tar.gz
编译主机:192.168.56.122
目录:/media/sf_share/code/protobuf
解压:tar -xzvf protobuf-all-3.5.1.tar.gz
解压后文件:protobuf-3.5.1
在目录 /media/sf_share/code/protobuf 下创建2个文件:
arm_protobuf
pc_protobuf
编译pc版本:
进入目录:/media/sf_share/code/protobuf/protobuf-3.5.1
配置参数:./configure --prefix=/media/sf_share/code/protobuf/pc_protobuf
编译(耐心等待):make
安装:make install
成功安装后 /media/sf_share/code/protobuf/pc_protobuf 有3个文件:
bin include lib
编译海思版本
进入目录:/media/sf_share/code/protobuf/protobuf-3.5.1
配置参数:
./configure --host=arm-linux CC=arm-himix200-linux-gcc CXX=arm-himix200-linux-g++ --with-protoc=/media/sf_share/code/protobuf/pc_protobuf/bin/protoc --prefix=/media/sf_share/code/protobuf/arm_protobuf
注意配置参数如下:
–host=arm-linux
CC=arm-himix200-linux-gcc
CXX=arm-himix200-linux-g++
–with-protoc=/media/sf_share/code/protobuf/pc_protobuf/bin/protoc
–prefix=/media/sf_share/code/protobuf/arm_protobuf
清除前面的编译文件:make clean
编译(耐心等待):make
安装:make install
成功安装后 /media/sf_share/code/protobuf/arm_protobuf 有3个文件:
bin include lib
测试代码:
msg.proto文件:
syntax = "proto3";
package lm;
message helloworld
{
int32 id = 1;
string str = 2;
int32 opt = 3;
}
write.cc文件:
#include "msg.pb.h"
#include <fstream>
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void)
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
printf("hello world\n");
lm::helloworld msg1;
msg1.set_id(1688);
msg1.set_str("zhoujinxing");
fstream output("./log",ios::out | ios::trunc | ios::binary);
if(!msg1.SerializeToOstream(&output)){
cerr << "Failed to write msg" << endl;
return -1;
}
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
reader.cc文件:
#include "msg.pb.h"
#include <fstream>
#include <iostream>
#include <stdio.h>
using namespace std;
void ListMsg(const lm::helloworld & msg){
cout << "Hello world, this is reader ..." << endl;
cout << msg.id() << endl;
cout << msg.str() << endl;
}
int main(void)
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
printf("hello world, reader ...\n");
lm::helloworld msg1;
{
fstream input("./log",ios::in | ios::binary);
if(!msg1.ParseFromIstream(&input)){
cerr << "Failed to parse address book." <<endl;
return -1;
}
}
/*
msg1.set_id(1688);
msg1.set_str("zhoujinxing");
fstream output("./log",ios::out | ios::trunc | ios::binary);
if(!msg1.SerializeToOstream(&output)){
cerr << "Failed to write msg" << endl;
return -1;
}
*/
ListMsg(msg1);
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
PC机编译运行
1,Makefile文件编辑
CFLAGS=-I/media/sf_share/code/protobuf/pc_protobuf/include -L/usr/lib -L/media/sf_share/code/protobuf/pc_protobuf/lib
#export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/media/sf_share/code/protobuf/pc_protobuf/lib/pkgconfig
#export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/media/sf_share/code/protobuf/pc_protobuf/lib
all:write reader
clean:
rm -f write reader *.o msg.p*.cc *.h log
proto_msg:
/media/sf_share/code/protobuf/pc_protobuf/bin/protoc msg.proto --cpp_out=.
write: msg.pb.cc write.cc
g++ msg.pb.cc write.cc -std=c++11 -o write $(CFLAGS) `pkg-config --cflags --libs protobuf` -lpthread
reader:msg.pb.cc reader.cc
g++ msg.pb.cc reader.cc -std=c++11 -o reader $(CFLAGS) `pkg-config --cflags --libs protobuf` -lpthread
2,pkg_config_path 编译环境变量设置:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/media/sf_share/code/protobuf/pc_protobuf/lib/pkgconfig
3,LD_LIBRARY_PATH 运行环境设置:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/media/sf_share/code/protobuf/pc_protobuf/lib
4,生产protobuf编解码源码命令:make proto_msg
此时出现2个文件:msg.pb.cc msg.pb.h
5,编译源码:make
6,执行写动作:./write
生成文件:log
7,执行读动作:./reader
结果:
hello world, reader ...
Hello world, this is reader ...
1688
zhoujinxing
此时,PC端protobuf安装环境成功!!
华为海思目标机(3519A/3516D)编译运行
1,ubuntu16(32位环境)重新开一个命令窗口。
2,把PC机验证的代码复制一份:cp -r pc_test/ arm_test
3,设置编译环境:
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/media/sf_share/code/protobuf/arm_protobuf/lib/pkgconfig
4,Makefile编译器变更(g++变更为arm-himix200-linux-g++):
CFLAGS=-I/media/sf_share/code/protobuf/pc_protobuf/include -L/usr/lib -L/media/sf_share/code/protobuf/pc_protobuf/lib
all:write reader
clean:
rm -f write reader *.o msg.p*.cc *.h log
proto_msg:
/media/sf_share/code/protobuf/pc_protobuf/bin/protoc msg.proto --cpp_out=.
write: msg.pb.cc write.cc
arm-himix200-linux-g++ msg.pb.cc write.cc -std=c++11 -o write $(CFLAGS) `pkg-config --cflags --libs protobuf` -lpthread
reader:msg.pb.cc reader.cc
arm-himix200-linux-g++ msg.pb.cc reader.cc -std=c++11 -o reader $(CFLAGS) `pkg-config --cflags --libs protobuf` -lpthread
5,删除编译记录:make clean
6,生产protobuf编解码源码命令:make proto_msg
此时出现2个文件:msg.pb.cc msg.pb.h
注意:用的工具protoc是PC编译的产物
7,编译源码:make
8,此时执行文件 write和reader只能在海思目标机(35519A/3516D)运行。
9,将pc机目录 /media/sf_share/code/protobuf/arm_protobuf 下整个文件拷贝到目标机上(位置:/mnt/nfs/arm_protobuf),目标机存储空间不够的话,可以考虑搭建nfs共享服务(具体搜索ubuntu nfs),设置运行环境变量:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/mnt/nfs/arm_protobuf/lib
10,目标机执行写动作:./write
root@Huawei:/mnt/nfs/arm_test# ./write
hello world
11,目标机执行读动作:./reader
root@Huawei:/mnt/nfs/arm_test# ./reader
hello world, reader ...
Hello world, this is reader ...
1688
zhoujinxing
此时,华为摄像头(海思3519A/3516D)的protobuf运行环境成功!!
本人反复安装protobuf多次了,之前每次都挺折腾的,没有详细记录好。这一次详细总结,除了方便自己日后查询,也希望能帮到其他朋友。