ProtoBuf编译生成Google.Protobuf.dll与Protoc.exe、自定义生成代码规则

ProtoBuf

基本介绍

  • 参考文章

    https://fungusfox.gitee.io/protobuf%E7%AC%AC%E4%BA%8C%E5%BC%B9/

  • 基本介绍

    Protocol buffers是一种可以将自定义的结构体序列化(反序列化)成字符流(byte[])的中间工具。

    对比JsonXML二进制,在序列化速度上更快,占用内存上更小,但相对而言操作更麻烦。

    由于Proto工具提供的语言支持,支持用户可以在不同的语言中对同样数据结构进行序列化和反序列化。

  • 本文目的

    下载Protobuf开源库,编译生成使用C#语言序列化和反序列化对应所需的dll(Google.Protobuf.dll)和应用程序(protoc.exe)

ProtoBuf导入到本地

  1. 打开开源地址:点这

  2. 点击Release

    image-20231108212141200

  3. 下载zip

    image-20231108212155607

    image-20231108212206671

  4. 本地解压

    image-20231108212314588

编译得到Google.Protobuf.dll

  1. 进入protobuf-25.0\protobuf-25.0\csharp\src

  2. 打开解决方案

    请添加图片描述

  3. 编译生成

    image-20231108221600759

  4. 会得到目标dll

    image-20231108221650745

编译得到Protoc.exe可执行文件

CMake编译ProtoBuf成Visual Studio

基本配置

  1. 本地安装CMake

    点这

    image-20231108212525030

  2. 打开CMake,配置ProtoBuf CMake工程路径和要生成的visual studio解决方案路径

    image-20231108212732511

  3. 点击Configure,配置要生成的vs解决方案版本,并点击finish

    image-20231108212826179

解决Finish后的错误

报错1及解决:googletest
  • 报错信息

     Cannot find third_party/googletest directory that's needed to build tests.
      If you use git, make sure you have cloned submodules:
    git submodule update --init --recursive
      If instead you want to skip tests, run cmake with:
        cmake -Dprotobuf_BUILD_TESTS=OFF
    Call Stack (most recent call first):
      CMakeLists.txt:336 (include)
    
  • 如何解决

    • 编辑根目录下的cmakelists.txt文件

      请添加图片描述

    • 定位到protobuf_BUILD_TESTS可选项

      image-20231108214252006

    • 将ON改成OFF

      请添加图片描述

    • 点击CMake的File菜单,删除缓存,再重新生成

      image-20231108215145281

  • 说明

    下载的protobuf.zip包中不含googletest子模块,而cmakelist.txt又设置为去编译googletest,简单做法通过宏设置不要编译googletest

报错2及解决:abseil-cpp
  • 报错信息

    protobuf_ABSL_PROVIDER is "module" but ABSL_ROOT_DIR is wrong
    Call Stack (most recent call first):
      CMakeLists.txt:294 (include)
    CMake Error at third_party/utf8_range/CMakeLists.txt:31 (add_subdirectory):
      The source directory
        G:/workspace/0.TestProject/protobuf-25.0/protobuf-25.0/third_party/abseil-cpp
      does not contain a CMakeLists.txt file.
    
  • 如何解决

    • Cmd进入\protobuf-25.0\third_party文件夹下

      请添加图片描述

    • 输入命令(本机需要有Git)

      git clone https://github.com/abseil/abseil-cpp
      

      克隆abseil-cpp仓库

      image-20231108215920563

    • 再重新生成即可

  • 说明

    编译protobuf的CMake设置变成IDE(Visual studio)的工程,abseil-cpp默认包含在CMake转换目标工程的列表中,但是下载的protobuf zip包中不含这个仓库,所以需要手动下载此仓库

Visual Studio生成exe基本操作

  1. 打开生成的visual studio工程解决方案

    image-20231108220454019

  2. 可选:对csharp_reflection_class.cc文件将proto文件生成cs的代码根据需求修改

    image-20231108222527467

  3. 对protoc项目进行生成

    请添加图片描述

    等待3 4分钟左右

    image-20231108221334454

  4. 会在解决方案的当前目录生成Debug/exe应用程序

    image-20231108221311685

使用Protoc.exe编译Proto文件成C#文件

  • 进入protobuf-25.0\Visual studio\Debug文件夹

    新建user.proto文件

    syntax = "proto3";//标明proto版本
     
    package protobuf;//包名
    
    message user {
      int32 userid = 1;               
      string name = 2;
      repeated string schools = 3;
    }
    

    请添加图片描述

  • cmd.exe执行命令

    protoc.exe user.proto --csharp_out=./
    

    image-20231108223551298

  • 查看生成的c#文件

    image-20231108224156683

    红框对应第2步:对csharp_reflection_class.cc文件将proto文件生成cs的代码根据需求修改

操作过程中的其它报错

  • 报错信息

    无法找到 global.json 指定的 .NET SDK 版本“7.0.202”,请检查是否已安装指定的
    
  • 如何解决

    • cmd 输入dotnet --info

      image-20231108213829463

    • 打开protobuf文件夹下的global.json

      image-20231108213853454

    • 将里面的版本改成7.0.100

      image-20231108213946780

      改成

      image-20231108213956993

      7.0.100对应cmd查看本机的dotnet版本

猜你喜欢

转载自blog.csdn.net/qq_34060370/article/details/134300921