im之消息格式protobuf
一、protobuf安装编译
1.1 编译环境
- centos7
- gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
1.2 源码
- 源码包列表地址:点击
- 本次使用版本:点击下载protobuf-cpp-3.9.1.tar.gz
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