1*1b3f573fSAndroid Build Coastguard Worker# Protocol Buffers - Code Example 2*1b3f573fSAndroid Build Coastguard Worker 3*1b3f573fSAndroid Build Coastguard WorkerThis directory contains example code that uses Protocol Buffers to manage an 4*1b3f573fSAndroid Build Coastguard Workeraddress book. Two programs are provided for each supported language. The 5*1b3f573fSAndroid Build Coastguard Workeradd_person example adds a new person to an address book, prompting the user to 6*1b3f573fSAndroid Build Coastguard Workerinput the person's information. The list_people example lists people already in 7*1b3f573fSAndroid Build Coastguard Workerthe address book. The examples use the exact same format in all three languages, 8*1b3f573fSAndroid Build Coastguard Workerso you can, for example, use add_person_java to create an address book and then 9*1b3f573fSAndroid Build Coastguard Workeruse list_people_python to read it. 10*1b3f573fSAndroid Build Coastguard Worker 11*1b3f573fSAndroid Build Coastguard WorkerThese examples are part of the Protocol Buffers tutorial, located at: 12*1b3f573fSAndroid Build Coastguard Worker https://developers.google.com/protocol-buffers/docs/tutorials 13*1b3f573fSAndroid Build Coastguard Worker 14*1b3f573fSAndroid Build Coastguard Worker## Build the example using bazel 15*1b3f573fSAndroid Build Coastguard Worker 16*1b3f573fSAndroid Build Coastguard WorkerThe example requires bazel 0.5.4 or newer to build. You can download/install 17*1b3f573fSAndroid Build Coastguard Workerthe latest version of bazel from bazel's release page: 18*1b3f573fSAndroid Build Coastguard Worker 19*1b3f573fSAndroid Build Coastguard Worker https://github.com/bazelbuild/bazel/releases 20*1b3f573fSAndroid Build Coastguard Worker 21*1b3f573fSAndroid Build Coastguard WorkerOnce you have bazel installed, simply run the following command in this examples 22*1b3f573fSAndroid Build Coastguard Workerdirectory to build the code: 23*1b3f573fSAndroid Build Coastguard Worker 24*1b3f573fSAndroid Build Coastguard Worker $ bazel build :all 25*1b3f573fSAndroid Build Coastguard Worker 26*1b3f573fSAndroid Build Coastguard WorkerThen you can run the built binary: 27*1b3f573fSAndroid Build Coastguard Worker 28*1b3f573fSAndroid Build Coastguard Worker $ bazel-bin/add_person_cpp addressbook.data 29*1b3f573fSAndroid Build Coastguard Worker 30*1b3f573fSAndroid Build Coastguard WorkerTo use protobuf in your own bazel project, please follow instructions in the 31*1b3f573fSAndroid Build Coastguard Worker[BUILD](BUILD) file and [WORKSPACE](WORKSPACE) file. 32*1b3f573fSAndroid Build Coastguard Worker 33*1b3f573fSAndroid Build Coastguard Worker## Build the example using make 34*1b3f573fSAndroid Build Coastguard Worker 35*1b3f573fSAndroid Build Coastguard WorkerYou must install the protobuf package before you can build it using make. The 36*1b3f573fSAndroid Build Coastguard Workerminimum requirement is to install protocol compiler (i.e., the protoc binary) 37*1b3f573fSAndroid Build Coastguard Workerand the protobuf runtime for the language you want to build. 38*1b3f573fSAndroid Build Coastguard Worker 39*1b3f573fSAndroid Build Coastguard WorkerYou can simply run "make" to build the example for all languages (except for 40*1b3f573fSAndroid Build Coastguard WorkerGo). However, since different language has different installation requirement, 41*1b3f573fSAndroid Build Coastguard Workerit will likely fail. It's better to follow individual instructions below to 42*1b3f573fSAndroid Build Coastguard Workerbuild only the language you are interested in. 43*1b3f573fSAndroid Build Coastguard Worker 44*1b3f573fSAndroid Build Coastguard Worker### C++ 45*1b3f573fSAndroid Build Coastguard Worker 46*1b3f573fSAndroid Build Coastguard WorkerYou can follow instructions in [../src/README.md](../src/README.md) to install 47*1b3f573fSAndroid Build Coastguard Workerprotoc and protobuf C++ runtime from source. 48*1b3f573fSAndroid Build Coastguard Worker 49*1b3f573fSAndroid Build Coastguard WorkerThen run "make cpp" in this examples directory to build the C++ example. It 50*1b3f573fSAndroid Build Coastguard Workerwill create two executables: add_person_cpp and list_people_cpp. These programs 51*1b3f573fSAndroid Build Coastguard Workersimply take an address book file as their parameter. The add_person_cpp 52*1b3f573fSAndroid Build Coastguard Workerprograms will create the file if it doesn't already exist. 53*1b3f573fSAndroid Build Coastguard Worker 54*1b3f573fSAndroid Build Coastguard WorkerTo run the examples: 55*1b3f573fSAndroid Build Coastguard Worker 56*1b3f573fSAndroid Build Coastguard Worker $ ./add_person_cpp addressbook.data 57*1b3f573fSAndroid Build Coastguard Worker $ ./list_people_cpp addressbook.data 58*1b3f573fSAndroid Build Coastguard Worker 59*1b3f573fSAndroid Build Coastguard WorkerNote that on some platforms you may have to edit the Makefile and remove 60*1b3f573fSAndroid Build Coastguard Worker"-lpthread" from the linker commands (perhaps replacing it with something else). 61*1b3f573fSAndroid Build Coastguard WorkerWe didn't do this automatically because we wanted to keep the example simple. 62*1b3f573fSAndroid Build Coastguard Worker 63*1b3f573fSAndroid Build Coastguard Worker### Python 64*1b3f573fSAndroid Build Coastguard Worker 65*1b3f573fSAndroid Build Coastguard WorkerFollow instructions in [../README.md](../README.md) to install protoc and then 66*1b3f573fSAndroid Build Coastguard Workerfollow [../python/README.md](../python/README.md) to install protobuf python 67*1b3f573fSAndroid Build Coastguard Workerruntime from source. You can also install python runtime using pip: 68*1b3f573fSAndroid Build Coastguard Worker 69*1b3f573fSAndroid Build Coastguard Worker $ pip install protobuf 70*1b3f573fSAndroid Build Coastguard Worker 71*1b3f573fSAndroid Build Coastguard WorkerMake sure the runtime version is the same as protoc binary, or it may not work. 72*1b3f573fSAndroid Build Coastguard Worker 73*1b3f573fSAndroid Build Coastguard WorkerAfter you have install both protoc and python runtime, run "make python" to 74*1b3f573fSAndroid Build Coastguard Workerbuild two executables (shell scripts actually): add_person_python and 75*1b3f573fSAndroid Build Coastguard Workerlist_people_python. They work the same way as the C++ executables. 76*1b3f573fSAndroid Build Coastguard Worker 77*1b3f573fSAndroid Build Coastguard Worker### Java 78*1b3f573fSAndroid Build Coastguard Worker 79*1b3f573fSAndroid Build Coastguard WorkerFollow instructions in [../README.md](../README.md) to install protoc and then 80*1b3f573fSAndroid Build Coastguard Workerdownload protobuf Java runtime .jar file from maven: 81*1b3f573fSAndroid Build Coastguard Worker 82*1b3f573fSAndroid Build Coastguard Worker https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java 83*1b3f573fSAndroid Build Coastguard Worker 84*1b3f573fSAndroid Build Coastguard WorkerThen run the following: 85*1b3f573fSAndroid Build Coastguard Worker 86*1b3f573fSAndroid Build Coastguard Worker $ export CLASSPATH=/path/to/protobuf-java-[version].jar 87*1b3f573fSAndroid Build Coastguard Worker $ make java 88*1b3f573fSAndroid Build Coastguard Worker 89*1b3f573fSAndroid Build Coastguard WorkerThis will create the add_person_java/list_people_java executables (shell 90*1b3f573fSAndroid Build Coastguard Workerscripts) and can be used to create/display an address book data file. 91*1b3f573fSAndroid Build Coastguard Worker 92*1b3f573fSAndroid Build Coastguard Worker### Go 93*1b3f573fSAndroid Build Coastguard Worker 94*1b3f573fSAndroid Build Coastguard WorkerFollow instructions in [../README.md](../README.md) to install protoc. Then 95*1b3f573fSAndroid Build Coastguard Workerinstall the Go protoc plugin (protoc-gen-go): 96*1b3f573fSAndroid Build Coastguard Worker 97*1b3f573fSAndroid Build Coastguard Worker $ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest 98*1b3f573fSAndroid Build Coastguard Worker 99*1b3f573fSAndroid Build Coastguard WorkerThe "go install" command will install protoc-gen-go into the GOBIN 100*1b3f573fSAndroid Build Coastguard Workerdirectory. You can set the $GOBIN environment variable before 101*1b3f573fSAndroid Build Coastguard Workerrunning "go install" to change the install location. Make sure the 102*1b3f573fSAndroid Build Coastguard Workerinstall directory is in your shell $PATH. 103*1b3f573fSAndroid Build Coastguard Worker 104*1b3f573fSAndroid Build Coastguard WorkerBuild the Go samples with "make go". This creates the following 105*1b3f573fSAndroid Build Coastguard Workerexecutable files in the current directory: 106*1b3f573fSAndroid Build Coastguard Worker 107*1b3f573fSAndroid Build Coastguard Worker add_person_go list_people_go 108*1b3f573fSAndroid Build Coastguard Worker 109*1b3f573fSAndroid Build Coastguard WorkerTo run the example: 110*1b3f573fSAndroid Build Coastguard Worker 111*1b3f573fSAndroid Build Coastguard Worker ./add_person_go addressbook.data 112*1b3f573fSAndroid Build Coastguard Worker 113*1b3f573fSAndroid Build Coastguard Workerto add a person to the protocol buffer encoded file addressbook.data. The file 114*1b3f573fSAndroid Build Coastguard Workeris created if it does not exist. To view the data, run: 115*1b3f573fSAndroid Build Coastguard Worker 116*1b3f573fSAndroid Build Coastguard Worker ./list_people_go addressbook.data 117*1b3f573fSAndroid Build Coastguard Worker 118*1b3f573fSAndroid Build Coastguard WorkerObserve that the C++, Python, Java, and Dart examples in this directory run in a 119*1b3f573fSAndroid Build Coastguard Workersimilar way and can view/modify files created by the Go example and vice 120*1b3f573fSAndroid Build Coastguard Workerversa. 121*1b3f573fSAndroid Build Coastguard Worker 122*1b3f573fSAndroid Build Coastguard Worker### Dart 123*1b3f573fSAndroid Build Coastguard Worker 124*1b3f573fSAndroid Build Coastguard WorkerFirst, follow the instructions in [../README.md](../README.md) to install the Protocol Buffer Compiler (protoc). 125*1b3f573fSAndroid Build Coastguard Worker 126*1b3f573fSAndroid Build Coastguard WorkerThen, install the Dart Protocol Buffer plugin as described [here](https://github.com/dart-lang/dart-protoc-plugin#how-to-build-and-use). 127*1b3f573fSAndroid Build Coastguard WorkerNote, the executable `bin/protoc-gen-dart` must be in your `PATH` for `protoc` to find it. 128*1b3f573fSAndroid Build Coastguard Worker 129*1b3f573fSAndroid Build Coastguard WorkerBuild the Dart samples in this directory with `make dart`. 130*1b3f573fSAndroid Build Coastguard Worker 131*1b3f573fSAndroid Build Coastguard WorkerTo run the examples: 132*1b3f573fSAndroid Build Coastguard Worker 133*1b3f573fSAndroid Build Coastguard Worker```sh 134*1b3f573fSAndroid Build Coastguard Worker$ dart add_person.dart addressbook.data 135*1b3f573fSAndroid Build Coastguard Worker$ dart list_people.dart addressbook.data 136*1b3f573fSAndroid Build Coastguard Worker``` 137*1b3f573fSAndroid Build Coastguard Worker 138*1b3f573fSAndroid Build Coastguard WorkerThe two programs take a protocol buffer encoded file as their parameter. 139*1b3f573fSAndroid Build Coastguard WorkerThe first can be used to add a person to the file. The file is created 140*1b3f573fSAndroid Build Coastguard Workerif it does not exist. The second displays the data in the file. 141