aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/cpp/README.md
blob: 70418b44253a313480f5b1fa1fb90751b75d001a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#gRPC in 3 minutes (C++)

## Installation

To install gRPC on your system, follow the instructions here:
[https://github.com/grpc/grpc/blob/master/INSTALL](https://github.com/grpc/grpc/blob/master/INSTALL).

## Hello C++ gRPC!

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).

The example code for this and our other examples lives in the `examples`
directory. Clone this repository to your local machine by running the
following command:


```sh
$ git clone https://github.com/grpc/grpc.git
```

Change your current directory to examples/cpp/helloworld

```sh
$ cd examples/cpp/helloworld/
```


### Generating gRPC code

To generate the client and server side interfaces:

```sh
$ make helloworld.grpc.pb.cc helloworld.pb.cc
```
Which internally invokes the proto-compiler as:

```sh
$ protoc -I ../../protos/ --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin ../../protos/helloworld.proto
$ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto
```

### Client and server implementations

The client implementation is at [greeter_client.cc](examples/cpp/helloworld/greeter_client.cc).

The server implementation is at [greeter_server.cc](examples/cpp/helloworld/greeter_server.cc).

### Try it!
Build client and server:
```sh
$ make
```
Run the server, which will listen on port 50051:
```sh
$ ./greeter_server
```
Run the client (in a different terminal):
```sh
$ ./greeter_client
```
If things go smoothly, you will see the "Greeter received: Hello world" in the client side output.

## Tutorial

You can find a more detailed tutorial in [gRPC Basics: C++](examples/cpp/cpptutorial.md)