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("guoyilong@163.com");
p.SerializeToString(&data);

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

data.length() 为32 

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

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





### 回答1: 很高兴能够回答您的问题,Ubuntu下安装Protobuf的步骤如下:1. 确保安装了Git,GCC,和Make;2. 使用Git下载Protobuf的源代码;3. 进入Protobuf的源代码目录,使用./configure配置Protobuf;4. 使用make编译Protobuf;5. 使用make install安装Protobuf。 ### 回答2: 在Ubuntu下安装protobuf可以按照以下步骤进行操作: 1. 打开终端,使用以下命令更新软件包列表:sudo apt update 2. 确保GCC编译器已安装,可以使用以下命令检查是否已安装:gcc --version 3. 如果GCC未安装,可以使用以下命令进行安装:sudo apt-get install build-essential 4. 使用以下命令安装protobuf软件包:sudo apt-get install protobuf-compiler 5. 安装完成后,可以使用以下命令检查所安装的protobuf版本:protoc --version 6. 如果还需要安装protobuf的Python插件,在终端中运行以下命令:sudo apt-get install python-protobuf 7. 安装完成后,可以使用pip命令安装protobuf库的Python包:pip install protobuf 8. 如果需要使用其他语言的protobuf库,可以在相应语言的包管理器中安装protobuf相关包。 以上步骤完成后,就可以在Ubuntu系统中使用protobuf库进行编译和使用了。安装protobuf可以让你更加方便地进行序列化和反序列化操作,加快数据传输速度。 ### 回答3: 在Ubuntu下安装protobuf可以通过以下步骤进行: 1. 打开终端,在命令行中输入以下命令来更新系统软件包列表: sudo apt-get update 2. 安装protobuf编译所需的依赖项,输入以下命令: sudo apt-get install autoconf automake libtool curl make g++ unzip 3. 下载protobuf源代码文件,可以从Github上的protobuf发布页面(https://github.com/protocolbuffers/protobuf/releases)选择合适的版本,然后用wget命令下载源代码文件,例如: wget https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protobuf-all-3.17.3.tar.gz 4. 解压下载的源代码文件,使用以下命令: tar -zxvf protobuf-all-3.17.3.tar.gz 5. 进入解压后的目录,输入以下命令: cd protobuf-3.17.3 6. 首先运行配置脚本,输入以下命令: ./configure 7. 编译protobuf代码,输入以下命令: make 8. 安装protobuf,输入以下命令: sudo make install 9. 安装完成后,输入以下命令来确认protobuf的版本: protoc --version 以上步骤完成后,你就成功在Ubuntu下安装了protobuf。你可以在终端中使用protoc命令来编译protobuf文件。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值