如何编译安装protobuf 编译极其python版本

15985人阅读
C/C++(6)
Google Protobuf
【准备工作】
Google Protobuf安装包 &&
我是以root用户的身份来登录的,非root用户可以su命令登录root帐号,或者在需要权限的命令前面加sudo。
假设下载的是protobuf-2.1.0.tar.gz
protobuf-2.1.0.tar.gz&
cd protobuf-2.1.0&
./configure --prefix=/opt/protobuf & &(这里指定的路径可以是任意)
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命令的作用见&&&&/linux/article/accidence/technique/54.html&)
【简单的demo编译】
1、写pb文件(消息文件)
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、写序列化消息的进程
#include &msg.pb.h& &
#include &fstream& &
#include &iostream& &
int main(void) &&
& & test:: &&
& & 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.& && &&
& & & & return -1; &&
& & } & & & & &
& & return 0; &&
编译&writer.cc&
g++ &msg.pb.cc&writer.cc -o&writer &`pkg-config --cflags --libs protobuf` -lpthread
./writer & , & 会在本地生成log文件
4、写反序列化的进程
#include &msg.pb.h& &
#include &fstream& &
#include &iostream& &
void PrintMsg(const test::msg & obj) { & &
& & cout && obj.id() && &&
& & cout && obj.str() && &&
int main(int argc, char* argv[]) { &&
& & test:: &&&&
& & & & fstream input(&./log&, ios::in | ios::binary); &&
& & & & if (!obj.ParseFromIstream(&input)) { &&
& & & & & & cerr && &Failed to parse address book.& && &&
& & & & & & return -1; &&
& & & & } & & & &&
& & PrintMsg(obj); &&
g++ &msg.pb.cc reader.cc -o reader &`pkg-config --cflags --libs protobuf` -lpthread
5、Makefile
all : writer reader &
& & 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`&
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:276446次
积分:2626
积分:2626
排名:第12357名
原创:29篇
转载:29篇
评论:38条
(1)(1)(2)(2)(1)(1)(1)(2)(1)(2)(3)(1)(1)(3)(2)(4)(5)(2)(3)(1)(3)(3)(3)(2)(4)(3)(1)

我要回帖

更多关于 protobuf 交叉编译 的文章

 

随机推荐