使用protobuf()

Protobuf的简介请看这里:http://blog.youkuaiyun.com/program_think/archive/2009/05/31/4229773.aspx

哪我们来讲一下该如何使用

1,首先去谷歌网站上下载(https://github.com/google/protobuf) 我下载的是 2.5版本的

2,编译好工程,如图所示(挨个编译,注: test工程不需要编译),在编译protoc工程的时候, 可能报错,

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall google::protobuf::compiler::CommandLineInterface::CommandLineInterface(void)" (??0CommandLineInterface@compiler@protobuf@google@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall google::protobuf::compiler::CommandLineInterface::~CommandLineInterface(void)" (??1CommandLineInterface@compiler@protobuf@google@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall google::protobuf::compiler::CommandLineInterface::RegisterGenerator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class google::protobuf::compiler::CodeGenerator *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?RegisterGenerator@CommandLineInterface@compiler@protobuf@google@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAVCodeGenerator@234@0@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall google::protobuf::compiler::CommandLineInterface::RegisterGenerator(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class google::protobuf::compiler::CodeGenerator *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?RegisterGenerator@CommandLineInterface@compiler@protobuf@google@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0PAVCodeGenerator@234@0@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall google::protobuf::compiler::CommandLineInterface::AllowPlugins(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?AllowPlugins@CommandLineInterface@compiler@protobuf@google@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall google::protobuf::compiler::CommandLineInterface::Run(int,char const * const * const)" (?Run@CommandLineInterface@compiler@protobuf@google@@QAEHHQBQBD@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall google::protobuf::compiler::cpp::CppGenerator::CppGenerator(void)" (??0CppGenerator@cpp@compiler@protobuf@google@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall google::protobuf::compiler::cpp::CppGenerator::~CppGenerator(void)" (??1CppGenerator@cpp@compiler@protobuf@google@@UAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall google::protobuf::compiler::python::Generator::Generator(void)" (??0Generator@python@compiler@protobuf@google@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall google::protobuf::compiler::python::Generator::~Generator(void)" (??1Generator@python@compiler@protobuf@google@@UAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall google::protobuf::compiler::java::JavaGenerator::JavaGenerator(void)" (??0JavaGenerator@java@compiler@protobuf@google@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall google::protobuf::compiler::java::JavaGenerator::~JavaGenerator(void)" (??1JavaGenerator@java@compiler@protobuf@google@@UAE@XZ) referenced in function _main

解决方案:只需要在protoc的工程main文件中添加如下两下:

#pragma comment(lib, "libprotobuf.lib")  
#pragma comment(lib, "libprotoc.lib")  
再设置lib的路径: 工程熟属性-》链接-》添加静态库库目录即可(也就是debug目录)
即可,



3,编译好之后,在工程libprotobuf中生成debug文件,libprotobuf.lib,protoc.exe三个文件

4,工程libprotobuf中目录下,运行extract_includes.bat,产生include文件夹

5,新建一个工程 testProtoBuf, 将libprotobuf.lib,protoc.exe,include文件夹,这三个文件复制到新建工程的跟目录下,如图所示



5,配置新项目环境,如图所示



6,配置好环境,我们需要在跟目录下创建如下两个文件


7,运行build.bat文件,在上层目录产生两个文件person.pb.h,person.pb.cc,然后将其放到



8,在工程中新建一个cpp文件,如图所示:


代码如下:

#include <iostream>
#include <fstream>
#include <string>
#include "person.pb.h"
#pragma comment (lib,"libprotobuf.lib")

using namespace std;
int main()
{
	Person person;
	person.set_id(6);
	person.set_email("adc");
	person.set_name("gan");

	/*
	文件序列化
	*/
	fstream out("person.xml", ios::out | ios::binary | ios::trunc);
	person.SerializeToOstream(&out);
	out.close();

	//从person.pb文件读取数据
	fstream in("person.xml", ios::in | ios::binary);
	if (!person.ParseFromIstream(&in)) {
		cerr << "Failed to parse person.xml." << endl;
		exit(1);
	}

	cout << "ID: " << person.id() << endl;
	cout << "name: " << person.name() << endl;
	if (person.has_email()) {
		cout << "e-mail: " << person.email() << endl;
	}


	return 0;
}


9,进行编译时,可能会出现一个错误


10.在工程属性中添加如下即可:_SCL_SECURE_NO_WARNINGS


资源下载链接为: https://pan.quark.cn/s/f989b9092fc5 ROS2是一个开源操作系统,用于构建智能机器人系统和自动化应用。它通过DDS实现数据通信,DDS是一种实时中间件,适用于高效、可靠的分布式系统通信。Protobuf是Google开发的数据序列化协议,以高效、紧凑的二进制格式被广泛用于数据存储和跨平台通信。在ROS2中应用protobuf,主要是为了提升数据传输效率和降低内存占用。将protobuf与DDS相结合,借助protobuf高效的序列化和反序列化机制以及DDS的发布/订阅模型,可增强ROS2的数据交换能力。 实现“ros2转protobuf,再转dds发出”需掌握以下知识点:一是protobuf基础,要知晓如何用protobuf定义消息结构,创建.proto文件,并借助protobuf编译器生成C++或Python代码,以便在程序中实现protobuf消息的创建、序列化与反序列化。二是ROS2接口与protobuf集成,ROS2消息类型一般用msg或srv定义,也可用protobuf定义,需编写适配器将protobuf消息与ROS2消息接口融合,转换数据格式。三是dds::topic::DataWriter和dds::topic::DataReader,这是DDS的核心接口,DataWriter用于发布数据,DataReader用于订阅数据,需创建DataWriter发布protobuf消息,创建DataReader接收并解析消息。四是ROS2的dds_idl工具,它可将protobuf消息转为DDS IDL,使消息能在DDS环境中使用。五是ROS2的idlpp工具,这是ROS2的编译器,用于处理idl文件,将其转为特定语言的源代码,在protobuf到DDS的转换中可能用到。六是自定义DDS middleware,因prot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值