调用python protobuf 安装pb2 必须安装吗

protobuf 安装中问题的解决 - 推酷
protobuf 安装中问题的解决
解决办法: 把自带的gtest 1.4 换成1.5
安装的版本是2.6.4 我在服务器的centos上安装是没问题的。但是我在我的ubuntu系统上安装的时候,make check是不成功的。然后google了下,说是gtest的版本问题。见这个:/p/protobuf/issues/detail?id=433。
然后我下载了个gtest 1.5编译安装,然后protobuf的make check也可以通过了。
报错的内容:
Making check in .
make[1]: Entering directory `/home/sisinc/Desktop/protobuf-2.4.1'
check-local
make[2]: Entering directory `/home/sisinc/Desktop/protobuf-2.4.1'
Making lib/libgtest.a lib/libgtest_main.a in gtest
make[3]: Entering directory `/home/sisinc/Desktop/protobuf-2.4.1/gtest'
depbase=`echo src/gtest.lo | sed 's|[^/]*$|.deps/&|;s|\.lo$||'`;\
/bin/bash ./libtool --tag=CXX
--mode=compile g++ -DHAVE_CONFIG_H -I. -I./build-aux
-I. -I./include
-g -DNDEBUG -MT src/gtest.lo -MD -MP -MF $depbase.Tpo -c -o src/gtest.lo src/gtest.cc &&\
mv -f $depbase.Tpo $depbase.Plo
libtool: compile:
g++ -DHAVE_CONFIG_H -I. -I./build-aux -I. -I./include -g -DNDEBUG -MT src/gtest.lo -MD -MP -MF src/.deps/gtest.Tpo -c src/gtest.cc
-fPIC -DPIC -o src/.libs/gtest.o
In file included from ./include/gtest/gtest-param-test.h:159:0,
from ./include/gtest/gtest.h:59,
from src/gtest.cc:34:
./include/gtest/internal/gtest-param-util-generated.h: In instantiation of ‘testing::internal::ValueArray2&T1, T2&::operator testing::internal::ParamGenerator&T&() const [with T = T1 = T2 = bool]’:
./include/gtest/gtest-param-test.h:1186:28:
required from here
./include/gtest/internal/gtest-param-util-generated.h:80:26: error: ‘ValuesIn’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
In file included from ./include/gtest/gtest.h:59:0,
from src/gtest.cc:34:
./include/gtest/gtest-param-test.h:288:58: note: ‘template&class Container& testing::internal::ParamGenerator&typename Container::value_type& testing::ValuesIn(const Container&)’ declared here, later in the translation unit
make[3]: *** [src/gtest.lo] Error 1
make[3]: Leaving directory `/home/sisinc/Desktop/protobuf-2.4.1/gtest'
make[2]: *** [check-local] Error 2
make[2]: Leaving directory `/home/sisinc/Desktop/protobuf-2.4.1'
make[1]: *** [check-am] Error 2
make[1]: Leaving directory `/home/sisinc/Desktop/protobuf-2.4.1'
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致使用protobuf 作为网络消息协议
使用protobuf 作为网络消息协议
#include &string&
#include &iostream&
#include &assert.h&
#include &stdint.h&
#include "person.pb.h"
#include "test.pb.h"
using namespace
class ProtoMsgHandle
注册消息处理函数
initHandles()
registerHandle(&ProtoMsgHandle::handleProtoPerson);
registerHandle(&ProtoMsgHandle::handleProtoTest);
处理网络消息
handle(const char* data)
bool ret = false;
const char* current =
int32_t* p_packetlen = (int32_t*)
current += sizeof(*p_packetlen);
int32_t* p_namelen = (int32_t*)
current += sizeof(*p_namelen);
const char* p_name =
string name(p_name, *p_namelen);
current += *p_
msg_handle callback = m_callbacks[name];
assert(callback != NULL);
if(callback == NULL)
const ::google::protobuf::Descriptor* descriptor = m_descriptors[name];
assert(descriptor != NULL);
if(descriptor == NULL)
const google::protobuf::Message* prototype = google::protobuf::MessageFactory::generated_factory()-&GetPrototype(descriptor);
assert(prototype != NULL);
if(prototype == NULL)
google::protobuf::Message* msg = prototype-&New();
ret = msg-&ParseFromArray(current, (*p_packetlen)-sizeof(*p_packetlen)-sizeof(*p_namelen)-*p_namelen);
(this-&*callback)(msg);
}while(0);
handleProtoTest(test* test)
cout && test-&price() &&
cout && test-&userid() &&
cout && test-&time() &&
handleProtoPerson(person* person)
cout && person-&age() &&
cout && person-&userid() &&
cout && person-&name() &&
typedef void (ProtoMsgHandle::*msg_handle)(::google::protobuf::Message*);
template&typename MSGTYPE&
registerHandle(void (ProtoMsgHandle::*callback)(MSGTYPE*))
const ::google::protobuf::Descriptor* des = MSGTYPE::descriptor();
assert(des != NULL);
if(des != NULL)
m_callbacks[des-&full_name()] = (msg_handle)
m_descriptors[des-&full_name()] =
map&string, msg_handle&
map&string, const ::google::protobuf::Descriptor*&
class ProtoMsgSender
发送proto msg到指定缓冲区
packet_len
proto_data
template&typename MSGTYPE&
sentProtoMsg(MSGTYPE& msg, char* buffer, int max_len)
char* current =
int32_t* p_packetlen = (int32_t*)
current += sizeof(*p_packetlen);
int32_t* p_namelen = (int32_t*)
current += sizeof(*p_namelen);
string msg_name = MSGTYPE::descriptor()-&full_name();
*p_namelen = msg_name.size();
strcpy(current, msg_name.c_str());
current += msg_name.size();
判断是否成功
msg.SerializeToArray(current, max_len - (current-buffer));
current += msg.GetCachedSize();
*p_packetlen = (current-buffer);
int main()
msghandle.initHandles();
t.set_price(100.0);
t.set_userid(110);
t.set_time(123);
person.set_age(18);
person.set_userid(200508);
person.set_name("irons");
char tmp[10*1024];
msgsender.sentProtoMsg(t, tmp, sizeof(tmp));
msghandle.handle(tmp);
msgsender.sentProtoMsg(person, tmp, sizeof(tmp));
msghandle.handle(tmp);
cin.get();
message test
required int32 time = 1;
required int32 userid = 2;
required float price = 3;
optional string desc = 4;
message person
required int32 age = 1;
required int32 userid = 2;
optional string name = 3;
发表评论:
馆藏&20411
TA的最新馆藏零散知识(6)
在前一篇文章中,对ProtoBuf做了一个基本介绍,这篇文章主要介绍ProtoBuf的安装和使用。
ProtoBuf的安装
安装ProtoBuf前需要先安装ProtoBuf的编译器和运行时环境。
对于C++使用者:由于ProtoBuf是用C++写的,因此你可以获取ProtoBuf的源码自己编译,而对于其他语言来说可以直接使用ProtoBuf已经编译好的Release版本直接进行安装和使用,相比来说使用Release版本会方便很多。
ProtoBuf在Git上的地址是:, 你可以clone源码然后按照ReadMe文件自己编译。
获取Release版本的地址是:&你可以从这个地址下载你所需要的安装包,注意使用C++编程的话下载cpp的包,其他语言对应选择,本文介绍了Linux下安装和使用ProtoBuf.
1、下载ProtoBuf的压缩包protobuf-cpp-3.0.0-beta-3.tar.gz 随便哪个版本都行
&&&& 将安装包拷贝到你的Linux机器上,进入所在目录,利用tar命令将该文件解压缩到当前文件夹。
&&& 加压后你可以再该目录下找到ReadME文件,这个文件就介绍了如何安装ProtoBuf,安装这部分基本是对该文件的一个中文翻译,英文水平好的可以直接参照该文件安装
2、安装ProtoBuf
&&&$ ./configure
&& $ make check
& &$ sudo make install
&& $ sudo ldconfig # refresh shared library cache.
进入你解压后的目录,依次执行以上命令,如果命令都执行成功,说明ProtoBuf安装成功。通常情况ProtoBuf都安装在/usr/local目录下,该目录下包含了ProtoBuf的头文件,静态库和动态库文件
如何使用ProtoBuf
ProtoBuf安装完成后,就可以按照上一篇文章介绍的步骤去使用ProtoBuf。(定义你自己的ProtoBuf源文件,用ProtoBuf编译器编译该源文件)
在本节内容中引用了Google官网中关于ProtoBuf使用的电话本的例子来介绍ProtoBuf的使用。
定义一个ProtoBuf源文件,Person.pro
syntax=&proto3&;
message Person
string name = 1;
int32 id = 2;
string email = 3;
enum PhoneType
MOBILE = 0;
message PhoneNumber
string number = 1;
PhoneType type = 2;
repeated PhoneNumber phone = 4;
message AddressBook
repeated Person person =1;
可以看到该文件中,包含了ProtoBuf结构的嵌套以及使用另外一种自定义的数据类型,Person结构中包含了一个Person联系人的基本信息,包括name, id ,email PhoneNumber
而PhoneNumber又是一个ProtoBuf的结构定义,某个属性被标注为repeated意味着编译后生成的Person对象中可以包含一个或者多个PhoneNumber也就是电话号码,而结构AddressBook中有包含了多个联系人Person 这个不难理解,跟我们实际手机联系人信息差不多,电话本中包含了多个联系人,每个联系人可以有多个电话号码,如家庭电话,手机,工作电话。
编译该源文件
&进图到源文件目录编译该源文件 protoc -I=。&& --cpp_out=.& Person.pro
编译后生成Person.proto,pb.h 和Person.pro.pb.cc文件,这两个文件包含了Person以及AddressBook的类,以及对应操作类的方法,在你使用者这些类必须包含头文件,最终可执行文件也必须将cc文件也编译进去。
下面展示使用Person的代码。
add_person.cpp 展示了如何设置一个联系人的各个字段的信息,并且最终序列化信息到文件中。
#include &Person.pro.pb.h&
#include &fstream&
#include &iostream&
void PromptForAddress(tutorial::Person*);
int main(int argc, char* argv[])
GOOGLE_PROTOBUF_VERIFY_VERSION;
if(2 != argc)
//必须指定电话本名称才执行程序
cerr && &Usage:
& && argv[0] && & ADDRESS_BOOK_FILE& &&
return -1;
tutorial::AddressBook address_
fstream in(&ADDRESS_BOOK_FILE&, ios::binary | ios::in);
cerr && &open file ADDRESS_BOOK_FILE failed!\n&;
return -1;
if(!address_book.ParseFromIstream(&in))
cerr && &Parse File ADDRESS_BOOK_FILE failed!\n&;
return -1;
in.close();
//增加一个Person,可通过多次调用该接口增加联系人
//具有repeated的属性可通过add_fieldname方法增加一个属性
PromptForAddress(address_book.add_person());
fstream out(&ADDRESS_BOOK_FILE&, ios::binary | ios::out | ios::trunc);
if(!address_book.SerializeToOstream(&out))
cerr && &Failed to Write Address Book!\n&;
return -1;
//可选的,回收所有ProtoBuf分配的对象
google::protobuf::ShutdownProtobufLibrary();
void PromptForAddress(tutorial::Person* person)
cout&&&Enter a Person ID number: &;
person-&set_id(id);
/*忽略CIN的前256个字符,或者忽略CIN的换行符之前的字符,包括换行符
这样的话不会将换行符之前的其他类型的数据保留在输入缓冲中
cin.ignore(256, '\n');
cout&&&Enter name: &;
getline(cin, *person-&mutable_name());
cout&& &Enter email address (blank for none): &;
getline(cin,email);
if(!email.empty())
person-&set_email(email);
while(true)
cout&&&Enter a phone number (or leave blank to finish): &;
getline(cin, number);
if(number.empty())
tutorial::Person::PhoneNumber* phone_number = person-&add_phone();
phone_number-&set_number(number);
cout&&&Is this a mobile, home, or work phone? &;
getline(cin, type);
if(type == &mobile&)
phone_number-&set_type(tutorial::Person::MOBILE);
else if( type == &home&)
phone_number-&set_type(tutorial::Person::HOME);
else if (type == &work&)
phone_number-&set_type(tutorial::Person::WORK);
cout && &Unknown phone type.
Using default.& &&
phone_number-&set_type(tutorial::Person::HOME);
list_cpp展示了如何从文件中读取ProtoBuf序列化的信息
#include &Person.pro.pb.h&
#include &iostream&
#include &fstream&
void ListPeople(const tutorial::AddressBook& address_book);
int main(int argc, char * argv[])
if(2!=argc)
cerr && &Usage:
& && argv[0] && & ADDRESS_BOOK_FILE& &&
return -1;
fstream in(&ADDRESS_BOOK_FILE&, ios::in | ios::binary);
tutorial::AddressBook address_
if(!address_book.ParseFromIstream(&in))
cerr && &Parse Input File failed!&&&
return -1;
ListPeople(address_book);
google::protobuf::ShutdownProtobufLibrary();
void ListPeople(const tutorial::AddressBook& address_book)
//fieldName_size方法返回具有repeated属性的个数
for(int i=0; i& address_book.person_size();i++)
const tutorial::Person& person = address_book.person(i);
cout&&&Person ID: &&&person.id();
cout&&&Name: &&&person.name();
cout&&&Email: &&&person.email();
for(int j=0; j& person.phone_size();j++)
const tutorial::Person::PhoneNumber& phone_number = person.phone(j);
switch(phone_number.type())
case tutorial::Person::MOBILE:
Mobile phone #: &;
case tutorial::Person::HOME:
Home phone #: &;
case tutorial::Person::WORK:
Work phone #: &;
cout&&phone_number.number()&&
因为是在Linux下运行的代码,因此写了一个makefile 文件来编译。
test:add_people list_people
add_people:add_person.cpp protoMid
add_person.cpp Person.pro.pb.cc -o add_people `pkg-config --cflags --libs protobuf`
list_people: list_person.cpp protoMid
list_person.cpp Person.pro.pb.cc -o list_people `pkg-config --cflags --libs protobuf`
protoMid: Person.pro
protoc -I=. --cpp_out=. ./Person.pro
rm -f add_people list_people protoMid
rm -f Person.pro.pb.cc Person.pro.pb.h
执行make后,生成add_people 和list_people两个可执行文件,可以先执行add_people文件增加联系人信息,再执行list_people显示所有联系人信息。
我在写makefile编译整个源文件时遇到了几个问题,一个是makefile中并没有显示的指定 -i -l 指定编译时需要的头文件地址和链接时需要指定的库文件地址,因此编译链接失败了,有两个方法可以解决该问题,一个是通过-i -l参数指定需要的头文件和库文件地址,另外一个是通过pkg-config 工具, 该工具通过PKG_CONFIG_PATH环境变量指定的地址去找.pc文件,该文件记录了ProtoBuf安装时头文件和库文件所在的目录。&
相比来说,第二种方法要方便很多,但是要注意的是要讲PKG_CONFIG_PATH环境变量设置到正确的地址,否则编译也会失败
$export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/
导出后,编译成功。
本文仅介绍了基本的ProtoBuf的安装和使用过程,对于入门的人来说本文应该能够让读者明白怎么去使用ProtoBuf结构化数据,但对于更加详细的本文没有提及,比如说ProtoBuf2和ProtoBuf3的区别,定义.proto文件的详细语法等,需要了解的可以参考google官方文档


参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:2957次
排名:千里之外
原创:23篇
(3)(2)(12)(4)(1)(6)(1)7871人阅读
google protocol buffer(3)
protobuf已经全面迁移到github,地址:
直接下载2.6.1版本:
我转linux不久所以对linux的各种系统路径不是特别熟悉,网上看了几个教程都没有提到
添加 LIBRARY_PATH路径,导致自定义安装路径的时候链接不过(尤其是非root用户默认安装的路径是没有权限的,需要修改安装安装路径),所以自己写一篇安装教程,也算作加深印象吧,希望对大家有所帮助。
默认安装步骤(需root权限):
$wget https:///google/protobuf/archive/v2.6.1.zip
$unzip protobuf-2.6.1.zip
$cd protobuf-2.6.1
下载自github的代码需要首先执行 $ ./autogen.sh 生成configure文件
$ ./configure
$ make check
$ make install
我使用的是centos系统
usr/local/bin
usr/local/lib,
usr/local/include
是也系统默认路径之一,所以到这一步就可以使用protobuf了
$ protoc -I=./ --cpp_out=./ test.proto
到你的test.proto文件所在目录使用命令protoc -I=./ --cpp_out=./ 生成C++版本的协议文件
一切OK的话,你回在当前目录看到.h和.cc文件
修改安装路径(非root用户需要修改安装路径):
protobuf默认安装在 /usr/local 目录
你可以修改安装目录通过 ./configure --prefix=命令
虽然我是root用户但觉得默认安装过于分散,所以统一安装在/usr/local/protobuf下
$./configure --prefix=/usr/local/protobuf
$ make check
$ make install
到此步还没有安装完毕,在/etc/profile 或者用户目录 ~/.bash_profile
添加下面内容
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib/
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/protobuf/lib/
export PATH=$PATH:/usr/local/protobuf/bin/
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/local/protobuf/include/
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/protobuf/include/
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/
好了,goog luck
额外赠送:
如果出现找不到符号和链接错误请记得加上链接选项 -lprotobuf
并确认你的静态库路径是否生效了
echo $LIBRARY_PATH
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:465288次
积分:5036
积分:5036
排名:第3886名
原创:57篇
转载:135篇
评论:100条
(1)(1)(1)(1)(5)(1)(3)(4)(10)(6)(1)(6)(7)(5)(1)(2)(1)(1)(1)(3)(4)(3)(8)(4)(10)(4)(4)(2)(6)(6)(6)(5)(3)(7)(6)(9)(10)(2)(1)(1)(13)(7)(2)(4)(8)如何在没有protobuf的环境下使用依赖protobuf的库?
[问题点数:60分]
如何在没有protobuf的环境下使用依赖protobuf的库?
[问题点数:60分]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
2013年7月 C/C++大版内专家分月排行榜第一
2015年9月 C/C++大版内专家分月排行榜第二2013年6月 C/C++大版内专家分月排行榜第二
2013年 总版技术专家分年内排行榜第三
2012年 总版技术专家分年内排行榜第七
2013年 总版技术专家分年内排行榜第三
2012年 总版技术专家分年内排行榜第七
本帖子已过去太久远了,不再提供回复功能。}

我要回帖

更多关于 windows安装protobuf 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信