im之消息格式protobuf

本文详细介绍了在CentOS7系统上如何安装和编译protobuf3.9.1,包括设置环境变量,以及使用protobuf编译proto文件并生成测试代码。在测试过程中,演示了数据的写入和读取操作,并解决了一个因缺少-lpthread链接选项导致的运行时错误。
摘要由CSDN通过智能技术生成

im之消息格式protobuf

一、protobuf安装编译

1.1 编译环境

  • centos7
  • gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

1.2 源码

1.3 安装编译

下载依赖项

sudo apt-get install autoconf automake libtool curl make g++ unzip

解压

tar -zvf protobuf-cpp-3.9.1.tar.gz

进入目录

cd protobuf-3.9.1

编译

make

测试

make check

安装

make install

1.4 设置环境变量

打开配置文件

 vim /etc/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

保存和生效

:wq
source /etc/profile

二、protobuf测试

2.1 版本测试

查询版本

[root@localhost protobuf-3.9.1]# protoc --version
libprotoc 3.9.1

3.1 生成测试文件

proto测试文件

syntax = "proto3";
enum Messagetype
{
	REQUEST_RESPONSE_NONE = 0;            
	REQUEST_HEARTBEAT_SIGNAL = 1;          
	RESPONSE_HEARTBEAT_RESULT = 2;      
}
message MsgResult
{
	bool result =1;  
	bytes error_code = 2; 
}
message TopMessage
{
	Messagetype message_type = 1; 		//message type
	MsgResult msg_result = 2;
}

转换格式

protoc ./test.proto --cpp_out=.

查看结果

[root@localhost protobuf_demo]# ls
test.pb.cc  test.pb.h  test.proto

3.2 测试运行demo

测试代码


#include "test.pb.h"		//解析出来的.h文件
#include "stdio.h"

void sendHeart();
void receHeart(TopMessage* topMessage);
void receHeartResp(TopMessage* topMessage);

/*
** ===================================================================
**     Method      :  sendHeart 
**
**     Description :  数据写入
** 
** ===================================================================
*/
void sendHeart(){
	
	TopMessage message;
	message.set_message_type(REQUEST_HEARTBEAT_SIGNAL);
	printf("sendHeart %d\n",message.message_type());
	receHeart(&message);
}

/*
** ===================================================================
**     Method      :  receHeart 
**
**     Description :  数据读取然后写入
** 
** ===================================================================
*/
void receHeart(TopMessage* topMessage){

	if (topMessage->message_type() == REQUEST_HEARTBEAT_SIGNAL)
	{
		
		printf("request_heartbeat_signal\n");
		TopMessage topMessageResp;

		MsgResult mesResult;
		mesResult.set_result(true);
	
		mesResult.set_error_code("error");
		
		topMessageResp.set_message_type(RESPONSE_HEARTBEAT_RESULT);
		
		*topMessageResp.mutable_msg_result() = mesResult;

		receHeartResp(&topMessageResp);
	}
	
}

/*
** ===================================================================
**     Method      :  receHeartResp 
**
**     Description :  数据读取
** 
** ===================================================================
*/
void receHeartResp(TopMessage* topMessage){

	if (topMessage->message_type() == RESPONSE_HEARTBEAT_RESULT)
	{
		printf("response_heartbeat_result\n");
		
		printf("%s\n",topMessage->msg_result().error_code().c_str());

	}
}

int main()
{
	sendHeart();
	google::protobuf::ShutdownProtobufLibrary();
}

编译方法

 g++ -std=c++11 test.pb.cc main.cpp -o test -I/usr/local/protobuf/include -L/usr/local/protobuf/lib -lprotoc -lprotobuf-lite -lprotobuf -lpthread

运行结果

[root@localhost protobuf_demo]# ./test
sendHeart 1
request_heartbeat_signal
response_heartbeat_result
error

三、常见问题

3.1 常见报错之运行报错

3.1.1 报错内容
[root@localhost protobuf_demo]# ./test
[libprotobuf FATAL google/protobuf/generated_message_util.cc:818] CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning): 
terminate called after throwing an instance of 'google::protobuf::FatalException'
  what():  CHECK failed: (scc->visit_status.load(std::memory_order_relaxed)) == (SCCInfoBase::kRunning): 
Aborted (core dumped)
3.1.2 出现条件

编译时未加入-lpthread

 g++ -std=c++11 test.pb.cc main.cpp -o test -I/usr/local/protobuf/include -L/usr/local/protobuf/lib -lprotoc -lprotobuf-lite -lprotobuf

文章参考:https://blog.csdn.net/baidu_32237719/article/details/99649451

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值