Saturday, May 14, 2016

[Language] Playing with protocol buffer


  1. Install Protocal Buffer
    1. Please read the readmes.  Running "make install" in the root directory installs the C++ libraries only.  You need to install the Java and Python versions separately by following the instructions in the corresponding directories. 
    2. https://groups.google.com/forum/#!topic/protobuf/60-OFrBXlAE
  2. How to start from protocol buffer

    1. Check example code from source code package. 
    2. https://github.com/google/protobuf/tree/master/examples
    3. Including source for java, c++, and python
  3. Examples
    1. CPP
      • protoc_middleman: addressbook.proto
      • protoc --cpp_out=. --java_out=. --python_out=. addressbook.proto
      • @touch protoc_middleman

      • protoc_middleman_go: addressbook.proto
      • mkdir tutorial # make directory for go package
      • protoc --go_out=tutorial addressbook.proto
      • @touch protoc_middleman_go

      • add_person_cpp: add_person.cc protoc_middleman
      • pkg-config --cflags protobuf  # fails if protobuf is not installed
      • c++ add_person.cc addressbook.pb.cc -o add_person_cpp `pkg-config --cflags --libs protobuf`

    1. pkg-config --cflags --libs protobuf 
      • pkg-config - Return metainformation about installed libraries
        • The  pkg-config program is used to retrieve information about installed libraries in
          •        the system.  It is typically used to compile and link against one or more libraries.
            •        Here is a typical usage scenario in a Makefile:

                •        program: program.c
                  •             cc program.c 'pkg-config --cflags --libs gnomeui'
                  1. `pkg-config --cflags --libs protobuf ` returns
                    1.  -lprotobuf -lz -lpthread on centos
                    2.  -pthread  -pthread -lprotobuf -lpthread on ubuntu 14.04
                    3. "Package protobuf was not found in the pkg-config search path." if it is not installed
                  2. Generated: 
                    1. add_person_cpp
                      1. Run this file, saved it to an address book following prompt
                      2. File looks like something like this
                    2. list_people_cpp
                      1. Run this file, returned with:
                        • Person ID: 1
                        •   Name: Charles
                        •   E-mail address: charles@gmail.com
                        •   Home phone #: 541 346 1380
                  3. Python
                    1. Error: ImportError: No module named google.protobuf
                      1. sudo pip install protobuf
                  4. Java
                    1. make Error:  package com.google.protobuf does not exist
                    2. need protobuf-java-*.jar

                No comments:

                Post a Comment