ProtoBuf compiles and generates Google.Protobuf.dll and Protoc.exe, and customizes the code generation rules

Article directory

  • ProtoBuf
    • basic introduction
    • Import ProtoBuf locally
  • Compile and get Google.Protobuf.dll
  • Compile to get the Protoc.exe executable file
    • CMake compiles ProtoBuf into Visual Studio
      • basic configuration
      • Resolve errors after Finish
        • Error 1 and solution: googletest
        • Error 2 and solution: abseil-cpp
    • Visual Studio generates basic exe operations
    • Use Protoc.exe to compile Proto files into C# files
  • Other errors during operation

ProtoBuf

Basic introduction

  • Reference article

    https://fungusfox.gitee.io/protobuf second version/

  • basic introduction

    Protocol buffers is an intermediate tool that can serialize (deserialize) custom structures into character streams (byte[]).

    Compared with Json, XML, and Binary, the serialization speed is faster and the memory usage is smaller, but the operation is relatively more troublesome.

    Due to the language support provided by the Proto tool, users can serialize and deserialize the same data structure in different languages.

  • Purpose of this article

    Download the Protobuf open source library, compile and generate the dll (Google.Protobuf.dll) and application (protoc.exe) required for serialization and deserialization using the C# language

Import ProtoBuf locally

  1. Open the open source address: click here

  2. Click Release

    image-20231108212141200

  3. Download zip

    image-20231108212155607

    image-20231108212206671

  4. Local decompression

    image-20231108212314588

Compile to get Google.Protobuf.dll

  1. Enter protobuf-25.0\protobuf-25.0\csharp\src

  2. Open solution

    Please add an image description

  3. Compile and generate

    image-20231108221600759

  4. Will get the target dll

    image-20231108221650745

Compile to obtain the Protoc.exe executable file

CMake compiles ProtoBuf into Visual Studio

Basic configuration

  1. Install CMake locally

    Click here

    image-20231108212525030

  2. Open CMake, configure the ProtoBuf CMake project path and the visual studio solution path to be generated

    image-20231108212732511

  3. Click Configure, configure the vs solution version to be generated, and click finish

    image-20231108212826179

Resolve errors after Finish

Error 1 and solution: googletest
  • Error message

     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)
    
  • How to solve

    • Edit the cmakelists.txt file in the root directory

      Please add a picture description

    • Locate the protobuf_BUILD_TESTS option

      image-20231108214252006

    • Change ON to OFF

      Please add a picture description

    • Click the File menu of CMake, delete the cache, and then regenerate it.

      image-20231108215145281

  • illustrate

    The downloaded protobuf.zip package does not contain the googletest submodule, and cmakelist.txt is set to compile googletest. A simple method is to not compile googletest through macro settings.

Error 2 and solution: abseil-cpp
  • Error message

    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.
    
  • How to solve

    • Cmd into the \protobuf-25.0\third_party folder

      Please add an image description

    • Enter the command (this machine needs to have Git)

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

      Clone abseil-cpp repository

      image-20231108215920563

    • Just regenerate it

  • illustrate

    The CMake settings for compiling protobuf become an IDE (Visual studio) project. abseil-cpp is included in the list of CMake conversion target projects by default. However, the downloaded protobuf zip package does not include this warehouse, so you need to download this warehouse manually.

Basic operations for generating exe with Visual Studio

  1. Open the generated visual studio project solution

    image-20231108220454019

  2. Optional: Modify the csharp_reflection_class.cc file to generate cs code from the proto file according to your needs.

    image-20231108222527467

  3. Generate protoc project

    Please add an image description

    Wait about 3 to 4 minutes

    image-20231108221334454

  4. Debug/exe application will be generated in the current directory of the solution

    image-20231108221311685

Use Protoc.exe to compile Proto files into C# files

  • Enter the protobuf-25.0\Visual studio\Debug folder

    Create a new user.proto file

    syntax = "proto3";//Indicate the proto version
     
    package protobuf;//package name
    
    message user {
      int32 userid = 1;
      string name = 2;
      repeated string schools = 3;
    }
    

    Please add image description

  • cmd.exe execute command

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

    image-20231108223551298

  • View the generated c# file

    image-20231108224156683

    The red box corresponds to step 2: modify the csharp_reflection_class.cc file to generate cs code from the proto file according to needs

Other errors during operation

  • Error message

    The .NET SDK version "7.0.202" specified by global.json cannot be found. Please check whether the specified version has been installed.
    
  • How to solve

    • cmd enter dotnet –info

      image-20231108213829463

    • Open global.json in the protobuf folder

      image-20231108213853454

    • Change the version inside to 7.0.100

      image-20231108213946780

      Change to

      image-20231108213956993

      7.0.100 corresponds to cmd to check the dotnet version of this machine