2022-02-18 编译并使用protobuf完成c++序列化
C++ 编译 2022 完成 02 18 序列化 Protobuf
2023-09-27 14:25:42 时间
目录
摘要:
记录编译并使用protobuf完成c++序列化
编译protobuf:
protobuf地址:
https://github.com/protocolbuffers/protobuf/releases
备份地址:
protobuf-all-3.19.4.tar.gz-互联网文档类资源-CSDN下载
编译:
tar -xzvf protobuf-all-3.19.4.tar.gz
cd protobuf-3.19.4/
./configure
make all
make install
默认安装的protobuf库的位置:
/usr/local/lib
编译时依赖protobuf库
-L/usr/local/lib -lprotobuf
运行程序时需要提前指定动态库的查找位置:
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
使用protobuf:
文件目录结构:
.
├── agent
├── build.sh
├── Makefile
├── run_proto.sh
├── src
│ ├── base
│ ├── config
│ ├── http
│ │ └── httplib.h
│ ├── proc
│ ├── proto
│ │ ├── hello.pb.cc
│ │ ├── hello.pb.h
│ │ └── hello.proto
│ ├── router
│ │ ├── router.cc
│ │ ├── router.h
│ │ ├── router_test.cc
│ │ └── router_test.h
│ └── server
│ ├── run.cc
│ └── server.h
└── utest
└── ut_testData.sh
9 directories, 15 files
hello.proto:
syntax = "proto2";
message helloworld
{
required int32 id=1;
optional string str=2;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
optional string number = 1;
optional PhoneType type = 2;
}
repeated PhoneNumber phones = 4;
}
使用protoc生成c++协议文件:
protoc --cpp_out=. ./src/proto/hello.proto
- --cpp_out: 生成的cpp文件位置, 相对于后边跟随的proto位置
- 后边跟随指定的proto文件
测试proto的代码:
#include <iostream>
#include "../src/proto/hello.pb.h"
bool writeBuf(string file_path)
{
helloworld msg1; // 包名::Message名
helloworld::PhoneNumber *phone; // 注意嵌套Message使用指针
msg1.set_id(100);
msg1.set_str("200");
phone = msg1.add_phones();
phone->set_number("12345");
phone->set_type(helloworld_PhoneType_HOME); // 注意Enum的调用方式(所有的默认值相当于C++中的Const对象, 所以直接使用类调用)
// Write the new address book back to disk.
fstream output(file_path, ios::out | ios::trunc | ios::binary);
if (!msg1.SerializeToOstream(&output)) {
std::cerr << "Failed to write msg." << std::endl;
return false;
}
return true;
}
// 输出数据
void ListMsg(const helloworld & msg) {
cout << msg.id() << std::endl;
cout << msg.str() << std::endl;
for (int i =0; i<msg.phones_size(); i++)
{
// 注意此时调用嵌套类中的值不是用的指针方式.
cout << msg.phones(i).number() << std::endl;
}
}
// 读取文件,写入到protobuf生成的类中
bool readBuf(string file_path)
{
helloworld msg1;
{
fstream input(file_path, ios::in | ios::binary);
if (!msg1.ParseFromIstream(&input)) {
std::cerr << "Failed to parse address book." << std::endl;
return -1;
}
}
ListMsg(msg1);
}
int main()
{
string path = "../test.log";
writeBuf(path);
readBuf(path);
return 0;
}
root@localhost:~/work/ndb-influxdb-instance/src/agent# ldd agent
linux-vdso.so.1 (0x00007ffe49789000)
libprotobuf.so.30 => /usr/local/lib/libprotobuf.so.30 (0x00007f230780a000)
libz.so.1 => /lib64/libz.so.1 (0x00007f23075f3000)
libbrotlicommon.so.1 => /lib64/libbrotlicommon.so.1 (0x00007f23073d2000)
libbrotlienc.so.1 => /lib64/libbrotlienc.so.1 (0x00007f2307146000)
libbrotlidec.so.1 => /lib64/libbrotlidec.so.1 (0x00007f2306f39000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f2306ba4000)
libm.so.6 => /lib64/libm.so.6 (0x00007f2306822000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f230660a000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f23063ea000)
libc.so.6 => /lib64/libc.so.6 (0x00007f2306025000)
/lib64/ld-linux-x86-64.so.2 (0x00007f2307cb0000)
相关文章
- 编译原理 词法分析实验/课程设计(C++实现 附源程序下载)
- c++编译常见错误原因集中
- 【VS开发】C/C++预编译命令
- C/C++基础:Visual Studio(基于Windows的C++)【比CMake(基于Linux系统)高级的编译器(Level-3)】
- C++-Cmake指令:编译相关函数【target_×××_×××】【比如:target_include_directories(声明了编译目标时查找使用头文件的路径)】
- C/C++-编译器(二):Makefile【包装所有g++指令】【描述文件编译的相关规则,告诉make要完成的shell指令】【一份定义了源文件间依赖关系、如何编译各个源文件并生成可执行文件的说明书】
- C++二叉搜索树
- C++初阶-C/C++内存管理
- [转] 国外程序员整理的 C++ 资源大全
- 应用程序启动后修改自身EXE文件或自删除EXE文件(附VC++6.0源码)
- 【c++】输出文件的每个单词、行
- 【C++】C&C++结构体内存对齐
- c++虚函数注意事项
- C++将时间格式转换成秒数
- Java JNI 编程进阶 实例+c++数据类型与jni数据类型转换
- C++编译错误cannot have cv-qualifier
- 《C++ Primer 第5版》-13.2拷贝控制和资源管理-康奈尔笔记
- linux下编译C/C++ 程序
- 在CentOS 7中使用VS Code编译调试C++项目
- 如何在windows中使用cmd命令去编译,运行C++程序
- Android Studio2.2 使用CMake编译 C/C++
- 【错误记录】Ubuntu 下 VSCode 编译报错 ( 无法生成和调试,因为活动文件不是 C 或 C++ 源文件。终端进程启动失败(退出代码: -1)。终端将被任务重用,按任意键关闭。 )
- python与C/C++相互调用
- [linux下c++ debug神器]vscode+gdb(debug console下使用)
- Mingw版QtCreator调用VS编译的C++库的方法
- ODB(C++ ORM)用Mingw的完整编译过程
- 六款常用的linux C/C++ IDE
- C++中的构造函数,拷贝构造函数和赋值运算
- 在C++中用C编译函数-extern "C" {}
- 如何在WINDOWS下编译BOOST C++库 .