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