Ubuntu 下配置protobuf

最近想研究protobuf ,尝试了很多次都没有成功,我用的是ubuntu,在虚拟机下面的 ,protobuf 也用了很多版本但都没有成功。最终用的是2.5.0版本才成功,话不多说直接开始梳理一下配置的流程。
首先得到  protobuf 相应的包文件 ,在终端上输入如下
wget http://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz

下载完毕后进行解压 

tar zxvf protobuf-2.5.0.tar.gz 

进入到解压目录 

cd protobuf-2.5.0
进行执行

./configure  

中间可能会出错,估计是G++没装好,因为安装的时候要进行编译

安装G++    

apt-get install g++
另外最好把Vim、make 也装了,不然的后面的就很容易出问题,这些在其他教程上都没提到过,是个人的一点经验与大家分享一下

apt-get install vim

apt-get install make

./configure 成功之后,接下来 就如下几步

make 
make check
make install

安装完成后在终端下执行

vim ~/.profile

打开配置文件,在该文件中添加

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

然后保存退出,接下来执行

source ~/.profile

 是配置文件修改生效,最后执行

protoc --version

查看protobuf版本以测试是否安装成功

接下来的操作 可以参照如下 链接 ,他们写得非常好 


http://hahaya.github.io/2013/08/12/use-protobuf-in-c-plus-plus.html


http://www.ccvita.com/507.html


创建一个proto 文件 比如 msg.proto

package lm;   
message helloworld   
{   
    required int32     id = 1;  // ID     
    required string    str = 2;  // str    
    optional int32     opt = 3;  //optional field   
}

将消息文件msg.proto映射成cpp文件

protoc -I=. --cpp_out=. msg.proto

可以看到生成了
msg.pb.h 和msg.pb.cc

http://blog.csdn.net/wallwind/article/details/11499643

对其代码做了一些纠正

write cpp 

#include "msg.pb.h"
#include <iostream>
#include <fstream>
using namespace std;
using namespace lm;
int main(void)
{
    lm:helloworld msg1;
	msg1.set_id(101);
	msg1.set_str("hello");
	fstream output("./msg.pb",ios::out | ios::trunc | ios::binary);
	if( !msg1.SerializeToOstream(&output))
	{
		 cerr << "Failed to write msg." << endl;   
         return -1;  
	}
	return 0;
}

reader.cpp

#include "msg.pb.h"
#include <iostream>
#include <fstream>
using namespace std;
using namespace lm;

void listmsg(const lm::helloworld & msg)
{
	cout << msg.id() <<endl;
	cout << msg.str() <<endl;
}

int main(void)
{
    lm:helloworld msg1;
	fstream input("./msg.pb", ios::in | ios::binary);   
    if (!msg1.ParseFromIstream(&input)) {   
            cerr << "Failed to parse address book." << endl;   
            return -1;   
    }   
	listmsg(msg1);
	return 0;
}


Makefile

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.cpp
	g++  msg.pb.cc write.cpp -o write  `pkg-config --cflags --libs protobuf`  
  
reader: msg.pb.cc reader.cpp
	g++  msg.pb.cc reader.cpp -o reader  `pkg-config --cflags --libs protobuf` 


如果提示 make: Nothing to be done for 'all
则执行make clean
如果从头开始执行的话 
1.make clean
2.make proto_msg
3.make
package demo;  
message People
 { required string name = 1;   
required int32 id = 2;   
required string email = 3;  } 

demo::People p;
p.set_name("guoyilong");
p.set_id(i);
p.set_email("[email protected]");
p.SerializeToString(&data);

当 i = 0 里 也就是 set_id(0)时 有一个很奇怪的现象,具体如下

data.length() 为32 

strlen(data.c_str()); 则为 12 两个结果不一致

而i 不等于0 则不会出现这种情况 现在还是不解 记录一下 





发布了60 篇原创文章 · 获赞 32 · 访问量 23万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章