Compilation and use of Protobuf on Ubuntu 14

Source: Internet
Author: User
Tags diff git clone

Objective

Always know that Google Open source of a language-independent data exchange protocol: PROTOBUF. Just know that it's a different format than JSON and XML, and that's especially good for performance (this is true for Java and C + + implementations!). )

Recently idle down to see Google's protobuf related things, but Baidu out of a lot of things are outdated, I have to spend some time to Daoteng, so there is the following content.

    • Download source code and preparation work
$ sudo apt-get install autoconf automake Libtool Curl
$ git clone https://
    • Modify Autogen.sh

Because of the "you Know" reason, Autogen can't curl download to Gmock's source code package, so I put Gmock's package on my own github. Modify the autogen.sh and let it download the package on my GitHub

[Email protected]:~/protobuf/protobuf$ gitdiffdiff--git A/autogen.SHB/autogen.SHIndex 5b4c29f: F2abf77100755---a/autogen.SH+ + + B/autogen.SH@@ - to,7+ to,7@@fi# Directory is set to as an SVN external.ifTest! -e Gmock; Then   Echo "Google Mock not present. Fetching gmock-1.7.0 from the web ..."-Curl $curlopts-O https://googlemock.googlecode.com/files/gmock-1.7.0.zip+ Curl $curlopts-L-o gmock-1.7.0.zip https://github.com/peter-wangxu/gMock/archive/1.7.0.zip   Unzip-Q gmock-1.7.0.Zip   RMgmock-1.7.0.Zip   MVgmock-1.7.0Gmock

#把curl那一行替换成绿色的

    • Generate Configure File
$ ./autogen
    • Compiling and installing PROTOBUF
$./make makesudomakeinstall   sudo ldconfig # refresh shared library cache.

Note: The default is installed under "/usr/local/lib", on some platforms/usr/local/lib is not the default Ld_library_path variable, you can change the installation directory by the following command

$./configure--PREFIX=/USR

When you see a text similar to the following, the PROTOBUF Basic installation is complete

============================================================================testsuite Summary for Protocol Buffers 3.0.0-beta-2============================================================================6  # PASS:  6# SKIP:  00# FAIL:   0  00=========================================================== =================

The next step is to configure some of the Python language-related

    • Python support for installing PROTOBUF
CD python # located under Protobuf
sudoinstall

Note: If the above command fails, you can try to install the next PIP related package, can solve some Python package dependency problem

sudo Install Python-pip

The next step is to use PROTOBUF.

    • Compiling the. Proto file
Touch1   String 2 string 1 string 2 ;}
    • Generate a PY file for use in later Python
Protoc--python_out=. ./dataservice.proto
    • Use of Protobuf

Create a testdataservice.py file and put it in the following section

ImportSYSImportDATASERVICE_PB2#Create Protorow =DATASERVICE_PB2. Rowproto () Row.null_map= 1Row.column.append ("Wang") Row.column.append ("female") Row_str=row. Serializetostring ()Print"Row_str:", row_strtable=DATASERVICE_PB2. Tableproto () table.column.append ("name") Table.column.append ("Gender") table.row.append (row_str) table_str=table. Serializetostring ()#process ProtoTable_proto =DATASERVICE_PB2. Tableproto () Table_proto. Parsefromstring (TABLE_STR)Print "column:"PrintTable_proto.columnrow_str=Table_proto.row[0]row_proto=DATASERVICE_PB2. Rowproto () Row_proto. Parsefromstring (Row_str.encode ('UTF8'))Print "Row1:"PrintRow_proto.column

Run testdataserver.py

[Email protected]:~/protobuf/proto_test$ python testdataservice.pyrow_str:wangfemalecolumn:[u' name ', u'gender']row1:[u'Wang', u ' female ']

The content of this issue is this, mainly PROTOBUF installation and configuration, the use of less involved, there will be time to add more use related content

FAQ:

If you encounter:

 while loading shared libraries:libprotoc.so. Ten Object file file or directory

Solution Solutions

sudo ldconfig

Reference article:

Https://github.com/google/protobuf

http://blog.csdn.net/whuqin/article/details/8730026

Compilation and use of Protobuf on Ubuntu 14

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.