Skip to content

Commit

Permalink
Add protobuf with C++
Browse files Browse the repository at this point in the history
  • Loading branch information
eightplus committed Sep 11, 2019
1 parent 3904e3b commit 9bd188a
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 1 deletion.
2 changes: 1 addition & 1 deletion code/C/my-plugin-daemon/README
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sudo apt-get install autotools-dev intltool libpolkit-agent-1-dev libpolkit-gobj


###
编译和运行(不是直接使用automake,而是使用mate-common提供的mate-autogen进行编译)
编译和运行(不是直接使用automake,而是使用mate-common提供的mate-autogen进行编译。通过命令autoconf –-version 查看系统autoconf版本号,修改configure.ac文件中的AC_PREREQ字段,如:AC_PREREQ([2.69]))

./autogen.sh --prefix=/usr

Expand Down
24 changes: 24 additions & 0 deletions code/C/protobuf/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#---- $@ 目标文件的名称 $^ 所有的依赖文件,以空格分开,不包含重复的依赖文件 $< 第一个依赖文件的名称 ----#

CXX = g++
CXXFLAGS += -std=c++14 -Wall -g
#LDFLAGS = -lprotobuf
LDFLAGS = `pkg-config --cflags --libs protobuf`

all: myinfo test_in test_out

myinfo:
protoc -I=./ --cpp_out=./ myinfo.proto

test_in:test_in.c myinfo.pb.cc
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)

test_out:test_out.c myinfo.pb.cc
$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)

clean:
rm -rf *.o
rm -rf test_in
rm -rf test_out
rm -rf myinfo.pb.*
rm -rf *.log
20 changes: 20 additions & 0 deletions code/C/protobuf/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# protobuf demo

Protobuf: Google Protocol Buffer 是 Google 公司内部的混合语言数据标准,用于 RPC 系统和持续数据存储系统。

#### 介绍
简要介绍 protobuf 在C++中的使用。

#### 依赖
libprotobuf-dev protobuf-compiler

###根据.proto生成对应的.cc和.h文件
protoc -I=./ --cpp_out=./ myinfo.proto

###编译
g++ -Wall -std=c++14 test_in.c myinfo.pb.cc -o test_in -lprotobuf
g++ -Wall -std=c++14 test_out.c myinfo.pb.cc -o test_out -lprotobuf

###运行
./test_in
./test_out
8 changes: 8 additions & 0 deletions code/C/protobuf/myinfo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
syntax = "proto2";
package com.xiaoming.protobuf;

message BufferMessage{
required int64 id = 1;
required string name = 2;
optional int32 opt = 3;
}
43 changes: 43 additions & 0 deletions code/C/protobuf/test_in.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2018 ~ 2020 [email protected]
*
* Authors:
* lixiang [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "myinfo.pb.h"

#include <fstream>
#include <iostream>

using namespace std;
using namespace com::xiaoming::protobuf;

int main(int argc, char* argv[])
{
BufferMessage msg;

msg.set_id(2019);
msg.set_name("lixiang");
msg.set_opt(1024);

fstream output("./protobuf.log", ios::out | ios::trunc | ios::binary);
if (!msg.SerializeToOstream(&output)){// SerializeToOstream 将对象序列化后写入一个 fstream 流
cerr << "save data error." << endl;
return -1;
}

return 0;
}
48 changes: 48 additions & 0 deletions code/C/protobuf/test_out.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2018 ~ 2020 [email protected]
*
* Authors:
* lixiang [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "myinfo.pb.h"
#include <fstream>
#include <iostream>

using namespace std;
using namespace com::xiaoming::protobuf;

void printMsg(const BufferMessage &msg)
{
cout << "id:" << msg.id() << endl;
cout << "name:" << msg.name() << endl;
if (msg.has_opt()) {
cout << "opt:" << msg.opt() << endl;
}
}

int main(int argc, char* argv[])
{
BufferMessage msg;
fstream input("./protobuf.log", ios::in | ios::binary);
if (!msg.ParseFromIstream(&input)){// ParseFromIstream 从一个 fstream 流中读取信息并反序列化
cerr << "read data from file error." << endl;
return -1;
}

printMsg(msg);

return 0;
}

0 comments on commit 9bd188a

Please sign in to comment.