-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |