blob: 85c495099b79b072198a69d5b7965ee632136828 [file] [log] [blame] [view]
LisaFCd1e11e72015-02-23 18:16:08 +00001#gRPC in 3 minutes (C++)
Abhishek Kumar3a5592c2015-02-20 15:32:42 -08002
LisaFCd1e11e72015-02-23 18:16:08 +00003## Installation
4
yang-gb00a3f62015-08-28 14:19:37 -07005To install gRPC on your system, follow the instructions [here](../../INSTALL).
Abhishek Kumar3a5592c2015-02-20 15:32:42 -08006
LisaFCd1e11e72015-02-23 18:16:08 +00007## Hello C++ gRPC!
Tim Emiola10135582015-02-23 09:22:26 -08008
yang-gb00a3f62015-08-28 14:19:37 -07009Here's how to build and run the C++ implementation of the [Hello World](../protos/helloworld.proto) example used in [Getting started](..).
Tim Emiola10135582015-02-23 09:22:26 -080010
Stanley Cheung0a268212015-08-27 14:38:38 -070011The example code for this and our other examples lives in the `examples`
12directory. Clone this repository to your local machine by running the
Tim Emiola10135582015-02-23 09:22:26 -080013following command:
14
15
16```sh
Stanley Cheung0a268212015-08-27 14:38:38 -070017$ git clone https://github.com/grpc/grpc.git
Tim Emiola10135582015-02-23 09:22:26 -080018```
19
Stanley Cheung0a268212015-08-27 14:38:38 -070020Change your current directory to examples/cpp/helloworld
Tim Emiola10135582015-02-23 09:22:26 -080021
22```sh
Stanley Cheung0a268212015-08-27 14:38:38 -070023$ cd examples/cpp/helloworld/
Tim Emiola10135582015-02-23 09:22:26 -080024```
25
Tim Emiola10135582015-02-23 09:22:26 -080026
Tim Emiola10135582015-02-23 09:22:26 -080027### Generating gRPC code
28
Tim Emiola10135582015-02-23 09:22:26 -080029To generate the client and server side interfaces:
30
31```sh
Nicolas "Pixel" Nobleb6413de2015-04-10 00:24:09 +020032$ make helloworld.grpc.pb.cc helloworld.pb.cc
Tim Emiola10135582015-02-23 09:22:26 -080033```
34Which internally invokes the proto-compiler as:
35
36```sh
Nicolas "Pixel" Nobleb6413de2015-04-10 00:24:09 +020037$ protoc -I ../../protos/ --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin ../../protos/helloworld.proto
38$ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto
Tim Emiola10135582015-02-23 09:22:26 -080039```
40
LisaFCd1e11e72015-02-23 18:16:08 +000041### Client and server implementations
Tim Emiola10135582015-02-23 09:22:26 -080042
yang-gb00a3f62015-08-28 14:19:37 -070043The client implementation is at [greeter_client.cc](helloworld/greeter_client.cc).
Tim Emiola10135582015-02-23 09:22:26 -080044
yang-gb00a3f62015-08-28 14:19:37 -070045The server implementation is at [greeter_server.cc](helloworld/greeter_server.cc).
Tim Emiola10135582015-02-23 09:22:26 -080046
LisaFCd1e11e72015-02-23 18:16:08 +000047### Try it!
Yang Gao3054eba2015-02-24 14:36:18 -080048Build client and server:
49```sh
50$ make
51```
Yang Gao84c263f2015-02-24 14:47:47 -080052Run the server, which will listen on port 50051:
Yang Gao3054eba2015-02-24 14:36:18 -080053```sh
54$ ./greeter_server
55```
Yang Gaoecee4242015-02-24 14:46:53 -080056Run the client (in a different terminal):
Yang Gao3054eba2015-02-24 14:36:18 -080057```sh
58$ ./greeter_client
59```
Yang Gaoecee4242015-02-24 14:46:53 -080060If things go smoothly, you will see the "Greeter received: Hello world" in the client side output.
LisaFCd1e11e72015-02-23 18:16:08 +000061
62## Tutorial
63
yang-gb00a3f62015-08-28 14:19:37 -070064You can find a more detailed tutorial in [gRPC Basics: C++](cpptutorial.md)