首页 > 其他分享 >使用protobuf()

使用protobuf()

时间:2023-08-28 15:37:21浏览次数:29  
标签:std google protobuf 使用 main class compiler


Protobuf的简介请看这里:

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

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目录)


即可,


使用protobuf()_ios

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

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

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

使用protobuf()_#include_02


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

使用protobuf()_#include_03

使用protobuf()_#include_04


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

使用protobuf()_java_05

使用protobuf()_#include_06

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

使用protobuf()_#include_07


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

使用protobuf()_java_08

代码如下:

#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,进行编译时,可能会出现一个错误

使用protobuf()_ios_09


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

使用protobuf()_ios_10

标签:std,google,protobuf,使用,main,class,compiler
From: https://blog.51cto.com/u_13566975/7264306

相关文章

  • 使用Python对HTTPS域名证书管理与验证
    随着业务的发展,很多域名都需要使用HTTPS。这就带来了一个新的问题:如何监控HTTPS域名证书的有效性。虽然证书不是一刹那过期的,但是也需要对其进行监控。了解其有效时间,并在过期前进行报警监控。要完成这些功能,所限就是要对证书进行解析。对证书解析可以使用python的OpenSSL库,以下为......
  • Git和Gitlab使用
    Git和Gitlab使用前言版本控制概念:记录开发文件的时间机器分类:1.本地版本控制系统、2.集中化的版本控制系统CVS、Subversion(SVN)、3.分布式版本控制系统GIT产品:github、git、gitlabGitlab部署1.介绍git是一个分布式的代码版本管理软件,而gitlab,gierrit,github都是git作......
  • MQTT服务器的搭建与MQTT客户端的使用
    一、MQTT服务器(emqx)搭建1、下载MQTTBroker官方下载地址:emqx-5.0.4-windows-amd64.tar.gz。百度网盘地址emqx-windows-4.3.10.zip:链接:https://pan.baidu.com/s/1XaPkWTI_AtYmWVuMD8d5HQ?pwd=n99m提取码:n99m其他版本选择:https://www.emqx.io/downloads?os=Windows。 2.不用......
  • Adobe Audition 2023(au2023)Mac+win中文永久使用版
    AdobeAudition2023是音频编辑软件AdobeAudition的最新版本,也是目前市场上最强大的音频编辑软件之一。它不仅拥有强大的音频编辑功能,还具备自动音频处理功能和高质量音频重建技术,为用户带来更为丰富的音频处理体验。→→↓↓载AdobeAudition2023 强大的音频处理功能在Ado......
  • .NET Core使用NPOI导出复杂Word详解
    前言:最近使用NPOI做了个导出Word文档的功能,关于使用.NETCore导出Word文档的方式有很多。最终我为什么选择了NPOI来实现了这个功能,首先是NPOI是一个开源,免费且容易上手的第三方框架(并且现在已支持.NETCore,GitHub源码地址:https://github.com/tonyqus/npoi)。因为之前使用NP......
  • [urlrewrite]使用urlrewrite进行地址自动重定向
    一般通过url访问网站,url的格式都是http://xxx.xxx.com?param=p1&param=p2 这是最传统的访问方式,但是,对于一些具有特殊要求的系统,其所有的页面的地址并不一定是这样子的格式,或者是类似于struts风格的.do的方式,比如http://xxx.xxx.com/aa.do其中的aa并不是action映射名称,而是其中一......
  • JasperReports+iReport在eclipse中的使用
    一、介绍1)它可以PDF,HTML,XML等多种形式产生报表或动态报表,在新版本还支持CSV,XLS,RTF等格式的报表;2)它按预定义的XML文档来组织数据,来源多(如:关系数据库,Java容器对象(collection,arrays)等);报表的填充过程:先产生报表设计对象->序列化该对象->存储在磁盘或网络-......
  • IDEA设置JAVA使用的编译语言Language level为8
       设置这个为8不然每次pom文件修改都会变更为5pom.xml增加<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>8</java.version></properties>  增加插件配置<!--打包配置--&g......
  • 【Java监控】使用SkyWalking监控Java服务
    你的Java服务是如何监控的呢? 1.Null:监控?什么监控?我一个写代码的服务挂了跟我有什么关系? 2.命令行:服务挂了?内存泄漏?jstatjmapjcmd,还好不是我写的3.撸代码:Java采集JVM/服务器资源信息->Prometheus->Grafana,请允许我对业务代码稍作修改。今天,给大家介绍一个对源码0入......
  • Vue3 使用Vuex与Vuex-persistedstate
    Vuex与vuex-persistedstateVuex是什么?Vuex是一个用于Vue.js应用程序的状态管理模式。它使得在应用程序中的所有组件之间共享和访问状态变得非常简单。Vuex将应用程序的状态存储在一个单一的存储库中,并且提供了一组用于更改状态的API。这使得状态管理变得更加可预测和易于调试。......