跳转至

grpc安装步骤

1
编译和运行grpc相关工程需要在工控机中配置grpc环境,包括一些可执行文件以及相关库,同时grpc还依赖大量的第三方库,安装过程较为繁琐,且容易出现纰漏。具体安装过程整理如下。

1. 下载grpc源码

代码获取方式有两种途径,一种是从github官网中下载,下载地址: grpc的编译需要依赖第三方库,在grpc/third_party目录下为所依赖子模块的源码,但是从github上不会直接将子模块下载到本地,还需要执行指令:

1
submodule update --init --recursive

实测该指令运行时间极慢,且容易中途崩溃,强烈建议修改.gitmodules文件中的下载源

1
2
vi .gitmodules
submodule update --init --recursive

第二种是从码云上下载,为了省去下载子模块所需要的时间,我们将grpc的源码以及三方库的源码全部迁移到了gitee上,下载地址:https://gitlab.rosc.org.cn/maqun001/grpc.git

2. 下载grpc编译依赖库

1
2
3
4
apt install -y build-essential autoconf libtool pkg-config
apt-get install autoconf automake libtool make g++ unzip 
apt-get install libgflags-dev libgtest-dev
apt-get install clang libc++-dev 

3. 编译grpc

1
grpc的编译需要依赖cmake与gcc,编译工具请自行安装

我们将编译的全部指令写在了grpc_config.sh脚本中,之后的编译过程就像把大象装进冰箱一样简单:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 1. 获得grpc_config.sh脚本
git clone
# 2. 执行脚本
./grpc_config.sh
# 3. 编译测试程序
cd grpc/
mkdir -p "examples/cpp/helloworld/cmake/build"
pushd "examples/cpp/helloworld/cmake/build"
cmake ../..
make
popd

4. 运行测试程序

1
2
3
4
cd examples/cpp/helloworld/cmake/build
./greeter_server
# 终端打印:
Server listening on 0.0.0.0:50051

开启新的终端:

1
2
3
4
cd examples/cpp/helloworld/cmake/build
./greeter_client
# 终端打印:
Greeter received: Hello World

安装成功!