Protobuf/C + + practical notes (1)

Source: Internet
Author: User

Objective:
Protobuf as a data interchange format, is liked by many people. High data compression ratio, strong backward compatibility, excellent performance, and platform-neutral, multi-lingual support (c + +, JAVA, Python). There are too many advantages, there are countless (home travel, arson essential medicine, OH yeah! ^_^).
This article focuses on compiling/linking and API usage for Linux/C + + version protobuf.

Protobuf Download and install
Let's use Protobuf 2.4.1 as a sample to show.
Community url:http://code.google.com/p/protobuf/
Download Link: http://protobuf.googlecode.com/files/protobuf-2.4.1.tar.gz
The following are the related commands and actions
1). Download and decompress
wget http://protobuf.googlecode.com/files/protobuf-2.4.1.tar.gz
TAR-JXVF protobuf-2.4.1.tar.gz
2). Compiling and installing
./configure--disable-shared--prefix=/path/to
Make && make install
3). Directory Structure
Tree-l 2 # Two-tier directory structure (BIN/INCULDE/LIB), as follows:
  
Commentary: Bin/protoc is a PB generation tool, include, Lib is the corresponding header file and the corresponding static/dynamic library

Actual Demo
Let's edit the Msg.proto file.

Commentary: simple definition of the msg_t class
  Use PROTOC to generate sequence/deserialization code for the corresponding language version

Protoc--cpp_out=./Msg.proto

Commentary:--cpp_out specifies the output path of the C + + version code
Final build msg.pb.cc msg.pb.h two file
Write the following test code:

#include "msg.pb.h" #include <stdio.h> #include <assert.h>int main () {char buf[1024] = {'} '};  int buf_len = 0;  msg_t MSG1;  MSG1.SET_ID (1001); *) Serialize phrase = = object to byte array msg1.  Serializetoarray (buf, sizeof (BUF)); Buf_len = Msg1.   ByteSize ();  msg_t MSG2; *) Deserialize phrase = = byte array to object MSG2.  Parsefromarray (buf, Buf_len);  ASSERT (msg1.id () = = Msg2.id ()); return 0;}

To compile and run
g++-o app app.cpp Msg.pb.cc-i/path/to/protobuf/include-l/path/to/protobuf/lib-lprotobuf-lpthread
./app
Commentary:/path/to for specific PROTOBUF installation directory

How to link
  static link or dynamic link ? This is a problem!
In the specified Protobuf library path, if there is a dynamic connection library, the compiled program takes precedence over the dynamic link , otherwise the static link is used .
Let's use a diagram to illustrate
  Dynamic Link Mode
In the Protobuf Lib directory, if there is a dynamic connection library (so file)
  
Then the compiled app executable program
Using LDD app Analytics, the following dependencies exist
  
Commentary: The Red Line area indicates a reference to the Dynamic Connection Library libprotobuf.so.7
Directly executes the binary app file and encounters the following error

./app:error while loading shared libraries:libprotobuf.so.7:cannot open Shared object file:no such file or directory

Obviously we need to set the ld_library_path variable here.

Export Ld_library_path=${ld_library_path}:/path/to/protobuf/lib

Commentary:/path/to/protobuf/lib for the actual protobuf installation path
  Static link mode
Simple removal of all dynamic connection library (so) files in the Lib directory,
  
Then compile and use the LDD analysis
  
Just execute the app and you're ready.

Summarize:
This is mainly about the compilation/installation of Protobuf, as well as the small demo writing, it is important to tell the difference between static link and dynamic link. On-line data are mostly dynamic links, but in fact, the need for static link is more direct.

Protobuf/C + + practical notes (1)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.