LisaFC | d1e11e7 | 2015-02-23 18:16:08 +0000 | [diff] [blame] | 1 | #gRPC in 3 minutes (C++) |
Abhishek Kumar | 3a5592c | 2015-02-20 15:32:42 -0800 | [diff] [blame] | 2 | |
LisaFC | d1e11e7 | 2015-02-23 18:16:08 +0000 | [diff] [blame] | 3 | ## Installation |
| 4 | |
| 5 | To install gRPC on your system, follow the instructions here: |
Abhishek Kumar | aa6c5fd | 2015-02-20 15:41:55 -0800 | [diff] [blame] | 6 | [https://github.com/grpc/grpc/blob/master/INSTALL](https://github.com/grpc/grpc/blob/master/INSTALL). |
Abhishek Kumar | 3a5592c | 2015-02-20 15:32:42 -0800 | [diff] [blame] | 7 | |
LisaFC | d1e11e7 | 2015-02-23 18:16:08 +0000 | [diff] [blame] | 8 | ## Hello C++ gRPC! |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 9 | |
Stanley Cheung | 0a26821 | 2015-08-27 14:38:38 -0700 | [diff] [blame^] | 10 | Here's how to build and run the C++ implementation of the [Hello World](examples/protos/helloworld.proto) example used in [Getting started](https://github.com/grpc/grpc/tree/master/examples). |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 11 | |
Stanley Cheung | 0a26821 | 2015-08-27 14:38:38 -0700 | [diff] [blame^] | 12 | The example code for this and our other examples lives in the `examples` |
| 13 | directory. Clone this repository to your local machine by running the |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 14 | following command: |
| 15 | |
| 16 | |
| 17 | ```sh |
Stanley Cheung | 0a26821 | 2015-08-27 14:38:38 -0700 | [diff] [blame^] | 18 | $ git clone https://github.com/grpc/grpc.git |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 19 | ``` |
| 20 | |
Stanley Cheung | 0a26821 | 2015-08-27 14:38:38 -0700 | [diff] [blame^] | 21 | Change your current directory to examples/cpp/helloworld |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 22 | |
| 23 | ```sh |
Stanley Cheung | 0a26821 | 2015-08-27 14:38:38 -0700 | [diff] [blame^] | 24 | $ cd examples/cpp/helloworld/ |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 25 | ``` |
| 26 | |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 27 | |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 28 | ### Generating gRPC code |
| 29 | |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 30 | To generate the client and server side interfaces: |
| 31 | |
| 32 | ```sh |
Nicolas "Pixel" Noble | b6413de | 2015-04-10 00:24:09 +0200 | [diff] [blame] | 33 | $ make helloworld.grpc.pb.cc helloworld.pb.cc |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 34 | ``` |
| 35 | Which internally invokes the proto-compiler as: |
| 36 | |
| 37 | ```sh |
Nicolas "Pixel" Noble | b6413de | 2015-04-10 00:24:09 +0200 | [diff] [blame] | 38 | $ protoc -I ../../protos/ --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin ../../protos/helloworld.proto |
| 39 | $ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 40 | ``` |
| 41 | |
LisaFC | d1e11e7 | 2015-02-23 18:16:08 +0000 | [diff] [blame] | 42 | ### Client and server implementations |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 43 | |
Stanley Cheung | 0a26821 | 2015-08-27 14:38:38 -0700 | [diff] [blame^] | 44 | The client implementation is at [greeter_client.cc](examples/cpp/helloworld/greeter_client.cc). |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 45 | |
Stanley Cheung | 0a26821 | 2015-08-27 14:38:38 -0700 | [diff] [blame^] | 46 | The server implementation is at [greeter_server.cc](examples/cpp/helloworld/greeter_server.cc). |
Tim Emiola | 1013558 | 2015-02-23 09:22:26 -0800 | [diff] [blame] | 47 | |
LisaFC | d1e11e7 | 2015-02-23 18:16:08 +0000 | [diff] [blame] | 48 | ### Try it! |
Yang Gao | 3054eba | 2015-02-24 14:36:18 -0800 | [diff] [blame] | 49 | Build client and server: |
| 50 | ```sh |
| 51 | $ make |
| 52 | ``` |
Yang Gao | 84c263f | 2015-02-24 14:47:47 -0800 | [diff] [blame] | 53 | Run the server, which will listen on port 50051: |
Yang Gao | 3054eba | 2015-02-24 14:36:18 -0800 | [diff] [blame] | 54 | ```sh |
| 55 | $ ./greeter_server |
| 56 | ``` |
Yang Gao | ecee424 | 2015-02-24 14:46:53 -0800 | [diff] [blame] | 57 | Run the client (in a different terminal): |
Yang Gao | 3054eba | 2015-02-24 14:36:18 -0800 | [diff] [blame] | 58 | ```sh |
| 59 | $ ./greeter_client |
| 60 | ``` |
Yang Gao | ecee424 | 2015-02-24 14:46:53 -0800 | [diff] [blame] | 61 | If things go smoothly, you will see the "Greeter received: Hello world" in the client side output. |
LisaFC | d1e11e7 | 2015-02-23 18:16:08 +0000 | [diff] [blame] | 62 | |
| 63 | ## Tutorial |
| 64 | |
Stanley Cheung | 0a26821 | 2015-08-27 14:38:38 -0700 | [diff] [blame^] | 65 | You can find a more detailed tutorial in [gRPC Basics: C++](examples/cpp/cpptutorial.md) |