Protobuf installation and use

Insert image description here

1 environment

ubuntn 20.04
protobuf v3.6.1

2 Installation [apt installation]

2 Installation [source code installation]

1 dependency

Requires git, g++, cmake, etc.

sudo apt-get update  
sudo apt-get install autoconf automake libtool

2 Download protobuf

Select version v3.6.1
URL: https://github.com/protocolbuffers/protobuf/releases/tag/v3.6.1
Select: protobuf-all-3.6.1.tar.gz

3 Unzip

Copy to your own directory and unzip it

sudo tar -zxvf protobuf-all-3.6.1.tar.gz

4 Compile and install

cd protobuf-3.6.1
sudo ./autogen.sh
#./configure --prefix=$INSTALL_DIR  #--prefix指定安装目录 默认 /usr/local
sudo ./configure --prefix=/opt/protobuf
sudo make
sudo make check
sudo make install

5 Configure environment

  • Add environment variables
vim /etc/profile
# 末尾加上如下两行
export PATH=$PATH:/opt/protobuf/bin/
export PKG_CONFIG_PATH=/opt/protobuf/lib/pkgconfig/
# 命令使生效
source /etc/profile
  • Configure the dynamic link library [you don’t need to configure it, just link it during compilation]
vim /etc/ld.so.conf

# 加入
/opt/protobuf/lib

# 动态库加载
sudo ldconfig

2 commands

View version

protoc --version

eg:libprotoc 3.6.1

uninstall

sudo apt-get remove libprotobuf-dev

3 use

Writing .proto files

The following naming rules facilitate understanding of
packageName.MessageName.proto

bp.test.proto

syntax = "proto3";

package BP;

message Test {
    
    
    int32 id = 1;		// ID
    string name = 2;	// name
}

message TestList {
    
    
    int32 id = 1;
    repeated Test tl = 2;
}

Compile .proto file to generate cpp file

After writing the proto file, you can use the Protobuf compiler to compile the file into the target language.

protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/XXX.proto

eg:
protoc --cpp_out=./ bp.test.proto

Generate two files [data operation, serialization and deserialization]
bp.test.pb.h, which defines the header file of the C++ class
bp.test.pb.cc and the implementation file of the C++ class

Write cpp file

#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include "./bp.test.pb.h"  
  
int main() {
    
      
    BP::Test t;  
    t.set_id(1);  
    t.set_name("sen");  
    printf("%d - %s\n", t.id(), t.name().c_str());  
  
    BP::Test t2 = t;  // 复制t到t2  
    t2.set_id(2);  
    printf("%d - %s\n", t2.id(), t2.name().c_str());
	
	BP::Test t3;  
    t3.set_id(3);  
    t3.set_name("sen3");  
    printf("%d - %s\n", t3.id(), t3.name().c_str());
  
    BP::TestList list;  
    list.set_id(007);  
    list.add_tl()->CopyFrom(t2);  // 复制t2到列表的第一个元素  
    printf("%d - %s\n", list.tl(0).id(), list.tl(0).name().c_str());  
  
    list.add_tl()->CopyFrom(t3);  // 复制t3到列表的第二个元素  
    printf("%d - %s\n", list.tl(1).id(), list.tl(1).name().c_str());  
  
    return 0;  
}

compile

g++ main.cpp bp.test.pb.cc -I /opt/protobuf/include -L /opt/protobuf/lib -lprotobuf -lpthread

Note:
Whether Protobuf relies on lpthread when compiling mainly depends on your build configuration. In some cases, such as when you build protobuf with some specific compiler or options, it may rely on lpthread. This is mainly because in some cases protobuf uses thread-local storage (TLS), which requires the lpthread library.

run

./a.out

1 - sen
2 - sen
3 - sen3
2 - sen
3 - sen3

reference

1. Detailed explanation of installing and using protobuf under Ubuntn
2. Installing Protobuf-2.5.0 in Ubuntu (details)

Guess you like

Origin blog.csdn.net/qq_38880380/article/details/135420860