Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
C++如何使用protobuf_C++数据序列化与protobuf库实践_创想鸟

C++如何使用protobuf_C++数据序列化与protobuf库实践

Protobuf通过定义.proto文件并编译生成C++代码实现高效序列化,使用SerializeToOstream和ParseFromIstream完成数据读写,结合g++或CMake链接库即可在项目中集成。

c++如何使用protobuf_c++数据序列化与protobuf库实践

在C++项目中高效处理结构化数据,Protobuf(Protocol Buffers)是一个被广泛采用的序列化方案。它由Google开发,具备高性能、跨平台、跨语言等优势。下面介绍如何在C++中使用Protobuf进行数据序列化与反序列化,并结合实际步骤演示完整流程。

定义.proto文件描述数据结构

使用Protobuf的第一步是编写.proto文件,用于定义要序列化的消息格式。

例如,创建一个person.proto文件:

syntax = "proto3";package tutorial;message Person {  string name = 1;  int32 age = 2;  string email = 3;}

这个定义描述了一个包含姓名、年龄和邮箱的Person结构。字段后的数字是唯一标识符(tag),用于二进制编码。

立即学习“C++免费学习笔记(深入)”;

编译.proto文件生成C++代码

使用protoc编译器将.proto文件转换为C++类。

执行以下命令:

protoc --cpp_out=. person.proto

会生成两个文件:person.pb.cc 和 person.pb.h。这些文件包含了Person类的定义,提供了序列化、反序列化、访问字段等方法。

在C++中序列化与反序列化数据

包含生成的头文件,在程序中创建并操作Person对象。

示例代码:

#include #include #include "person.pb.h"int main() {    // 设置日志输出(可选)    google::protobuf::LogToStderr();    tutorial::Person person;    person.set_name("Alice");    person.set_age(30);    person.set_email("alice@example.com");    // 序列化到文件    std::ofstream output("person.data", std::ios::binary);    if (!person.SerializeToOstream(&output)) {        std::cerr << "序列化失败!" << std::endl;        return -1;    }    output.close();    // 从文件反序列化    tutorial::Person restored_person;    std::ifstream input("person.data", std::ios::binary);    if (!restored_person.ParseFromIstream(&input)) {        std::cerr << "反序列化失败!" << std::endl;        return -1;    }    input.close();    // 输出恢复的数据    std::cout << "Name: " << restored_person.name() << std::endl;    std::cout << "Age: " << restored_person.age() << std::endl;    std::cout << "Email: " << restored_person.email() << std::endl;    return 0;}

编译与链接Protobuf库

编译上述代码需要链接Protobuf库。假设使用g++,命令如下:

g++ -o person_example person.pb.cc main.cpp -lprotobuf

确保系统已安装libprotobuf-dev(Linux)或正确配置了Protobuf的头文件与库路径(Windows/macOS)。若使用CMake,可添加:

find_package(Protobuf REQUIRED)target_link_libraries(your_target ${Protobuf_LIBRARIES})

基本上就这些。只要定义好结构、生成代码、调用SerializeToOstream和ParseFromIstream,就能实现高效的C++数据序列化。Protobuf还支持嵌套消息、repeated字段、默认值和兼容性升级,适合复杂场景下的通信与存储需求。

以上就是C++如何使用protobuf_C++数据序列化与protobuf库实践的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1484288.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
c++怎么实现一个定点数算术库_C++数值计算与定点数实现
上一篇 2025年12月19日 07:58:26
c++怎么处理跨平台的字节序问题_C++中大端与小端转换的兼容性处理方法
下一篇 2025年12月19日 07:58:38

相关推荐

发表回复

登录后才能评论
关注微信