Using VS Code to generate C++ Google Protocol Buffer project and usage records under Mac

Protobuf compilation and usage records under Mac

foreword

Compared with the plain text format of xml and json, the information transmission format is serialized, which obviously takes up less space, but it also makes reading more complicated.

Normal reading requires the support of third-party libraries for protobuf files, so record your own compilation and use steps.

Design ideas

The idea of ​​using protocal in C++ is to automatically create a header file through the program. After including the header file in the project, it can support the reading and writing of the protocal protocol. Therefore, when using it, it is necessary to write the content of the protocol protocol first, and then generate the corresponding header file and function body file according to the content. Therefore, when the content of the protocol protocol changes, the corresponding header file needs to be regenerated, which may lead to major changes in the project in severe cases.

A project that uses protobuf completely involves source code, compilation support tools, library files, and protocol conversion programs, and then protobuf can be referenced in the project.

Source files and compilation support tools are used to generate protocol conversion programs and library files;
protocol conversion programs and custom protocol content files can generate corresponding header files;
source files, library files, and header files corresponding to protocols can be included in the project.

usage record

The project address of Google Protocol Buffer is https://github.com/protocolbuffers/protobuf , and the C++ compilation reference is https://github.com/protocolbuffers/protobuf/tree/master/src .

Let me record my unofficial compilation steps here:

// 安装编译工具  // Mac可使用brew进行安装
// sudo apt-get install autoconf automake libtool curl make g++ unzip
sudo brew install autoconf automake libtool curl make g++ unzip
// 获取源文件
git clone https://github.com/protocolbuffers/protobuf.git
// 进入源文件目录
cd protobuf
// 更新子模块 // 貌似只与google test有关
git submodule update --init --recursive

I really think I'm the only one doing this, because I really can't find it on the Internet.

// 进入构建目录
cd cmake
// 创建生成目录
mkdir build
// 进入目标路径
cd build
// 创建编译文件
cmake ..
// 编译  // -j4编译加速,4代表使用4核心进行编译
make -j4

The operation under the protobuf project is over, and the protoc executable file and libprotobuf.a static library file will be generated in the cmake/build directory .

The operation records under VSCode are as follows:
1. Create the proto protocol content file msg.proto

syntax="proto3";
package lm; 
message helloworld 
{
    
     
    int32     id = 1;  // ID   
    string    str = 2;  // str  
    int32     opt = 3;  //optional field 
}

2. Generate read and write files in bash

// --cpp_out=. : 在当前目录生成c++文件
// msg.proto   : 协议文件为“msg.proto”
// 生成的文件名为”msg.pb.h“和”msg.pb.cc“
protoc --cpp_out=. msg.proto

3. Create the program entry file write.cpp

// g++ -o write write.cpp msg.pb.cc --std=c++11 -I. -L. -lprotobuf 
#include "msg.pb.h"
#include <fstream>
#include <iostream>
using namespace std;
int main(void)
{
    
    
    lm::helloworld msg1;
    msg1.set_id(101);
    msg1.set_str("hello");
    fstream output("./log", ios::out | ios::trunc | ios::binary);
    if (!msg1.SerializeToOstream(&output))
    {
    
    
        cerr << "Failed to write msg." << endl;
        return -1;
    }
    return 0;
}

4. Copy the "src/google" folder, "msg.pb.h" file, "msg.pb.cc" file, "libprotobuf.a" file, and "write.cpp" file in the git project directory to the same folder, and then execute the following compilation command.

// -I. :指定头文件目录为当前文件夹
// -L. :指定静态链接库目录为当前文件夹
// -lprotobuf :链接”libprotobuf.a“静态库文件  // "lib" + "protobuf" + ".a"
g++ -o write write.cpp msg.pb.cc --std=c++11 -I. -L. -lprotobuf

At this point, the target program can be executed

./write

Guess you like

Origin blog.csdn.net/u012101384/article/details/123007032