Protocol buffer two simple example c ++ Program

Source: Internet
Author: User

My example is to prove the good cross-language features of protocol buffer.

The example consists of twoProgramA c ++ program is responsible for serializing the object user to a file. The other is the Java program, which is responsible for deserializing from files into user objects.

The following figure shows the relationship between two programs and Protocol buffer.

First, design the proto file of the user object. The Protocol buffer compiler should use it to generate C ++ files and java files.

 
Package sample; Option java_package = "org. freebird. sample"; Option java_outer_classname = "userproto"; message user {required string name = 1; required string email = 2 ;}

For the generated C ++ files, the user class is located in the namespace sample
For the generated Java file, the compiler creates a class userproto, and the package name is org. freebird. sample. The userproto class has a public internal static class user

Assume that the sample. proto file is located in the template directory, and a build. Sh file is created under the directory:

Rm-rf c ++ mkdir C ++ Rm-RF javamkdir javaprotoc-I =. /-- cpp_out =. /C ++ -- java_out =. /Java. /sample. the proto script is very simple. The C ++ and Java directories will be created each time you execute the script, and the generated files will be placed under these two directories. The directory structure is as follows:
 
$ Tree templatetemplate ── build. sh ├ ── C ++ │ ├ ── sample. pb. CC │ ── sample. pb. h ├ ── Java │ org ── org │ └ ── freebird │ └ ── sample │ └ ── userproto. java ── sample. PROTO

Now let's look at the C ++ program. The directory structure of the cmake project is as follows:
$ Tree C ++ ── build ├ ── cmakelists.txt include ── include └ ── SRC ├ ── cmakelists.txt ── main. cpp
Import the required header file,Source codeFiles and libraries:

Cmake_minimum_required (version 2.8.7) Set (cmake_build_type Debug) Set (project_include_dir .. /include) Set (template_dir .. /.. /.. /template) templates ($ {template_dir}/C ++ $ {project_include_dir}) aux_source_directory ($ {region}/src cpp_list1) aux_source_directory ($ {template_dir}/C ++ cpp_list2) add_executable (sample ${cpp_list1 }$ {cpp_list2}) target_link_libraries (sample protobuf) add_definitions (-wall)

Compile the source of Main. cppCode:

# Include <iostream> # include <fstream> using namespace STD; # include "sample. pb. H "using namespace sample; int main (INT ARGs, char * argv []) {cout <" welcome to the C ++ sample for protocol buffer "<Endl; user user; user. set_name ("freebird"); User. set_email ("shu_chen@esri.com"); fstream output (argv [1], IOS: Out | IOs: trunc | IOs: Binary); If (! User. serializreceivstream (& output) {cerr <"failed to write user. "<Endl; Return-1 ;}cout <" write to file: "<argv [1] <" successfully "<Endl ;}

Run the program:

 
$./Sample./awelcome to the c ++ sample for protocol bufferwrite to file:./A successfully

Open result file

 
^ Hfreebird ^ r ^ Qshu_chen@esri.com

 

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.