A small trial of protobuf and protobuf-c

Introduction

A more lightweight data format - protobuf.

Advantages of protobuf:

  • Smaller, faster and simpler.
  • Supports multiple programming languages.
  • The parsing speed is fast.
  • Strong scalability.

the difference

It mainly does not include two functions:
because the languages ​​supported by protobuf are not complete enough, some languages ​​​​are not in this warehouse, so you need to download specific languages, then protobuf-c is the implementation that supports the C language version.
For the official version of protobuf, it includes support for python, ruby, C++ and other languages, but does not include C language:
1. protoc generates code for supported languages ​​through proto files
2. protobuf libraries in different languages ​​are used when the code is finally called

For the c language version of protobuf, it is only implemented for the C language, including:
1. protoc-c generates code corresponding to the c language through the proto file
2. The libprotobuf-c library is used for connection during compilation.

Install

Before downloading ProtoBuf, be sure to install the dependent libraries:
autoconf automake libtool curl make g++ unzip
If not installed, the installation command is as follows:

Ubuntu users choose:

sudo apt-get install autoconf automake libtool curl make g++ unzip -y

Get source code

protobuf version address: https://github.com/protocolbuffers/protobuf/releases
protobuf-c version address: https://github.com/protobuf-c/protobuf-c/releases

Compile and install

protobuf

You need to switch the main branch to the 4.0.x branch to have the autogen.sh script.

./autogen.sh

./configure is installed in the /usr/local directory by default, and lib and bin are scattered.

./configure --prefix=/usr/local/protobuf is installed in the /usr/local/protobuf directory

make -j4 && make install

That is to say, if the installation directory is modified, /etc/profilesome content needs to be added to the (system configuration file):

sudo vim /etc/profile

Add the following content:

#(Dynamic library search path) Specify a path other than the system default path when searching for a dynamic link library during program loading and running.

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib/

#(Static library search path) Specify the path to search for shared libraries when searching for dynamic link libraries during program compilation.

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/protobuf/lib/

#Execution program search path

export PATH=$PATH:/usr/local/protobuf/bin/

#cProgram header file search path

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/local/protobuf/include/

#c++ program header file search path

export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/usr/local/protobuf/include/

#pkg-config path

export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/

The last step is to re-execute the /etc/profile file:

source /etc/profile

protobuf-c

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
./autogen.sh
./configure --prefix=/usr/local/protobufc
make -j4 && make install

As above, you need to add the above configuration to the /etc/profile file.
Insert image description here

Use conversion tools

protoc-c --c_cout=. *.proto

Guess you like

Origin blog.csdn.net/yiyu20180729/article/details/132111081