ubuntu18下grpc编译与安装

grpc装起来很麻烦,会有很多坑。按照官方文档或者其他博客的步骤,不注意依赖库的版本页会导致各种问题。

0. 环境

在 https://grpc.io/docs/ grpc的环境依赖如下

LanguageOSCompilers / SDK
C/C++Linux, MacGCC 4.9+, Clang 3.4+
0.1 操作系统Ubuntu 18.04.4 LTS (腾讯云的虚拟机)

坑1:为了支持cpp11,gcc版本要4.9+,所以一些比较老的系统比如centos7.6,gcc默认4.8.5,是要升级gcc/g++的。升级gcc有两种方式

  • 编译安装,耗时长,软连接和libc的库需要手动更新,不推荐。
  • 使用centos-release-scl安装gcc, 升级方便快速
[root@VM-0-16-centos ~]# yum install centos-release-scl
[root@VM-0-16-centos ~]# yum install devtoolset-8-gcc*
[root@VM-0-16-centos ~]# scl enable devtoolset-8 bash
[root@VM-0-16-centos ~]# gcc -v
....
gcc version 4.9.0 (GCC) 

如果系统环境对自己不存在束缚,那么推荐直接上Ubuntu18,gcc目前版本是7.5,能够对开发有个良好的支撑,不用考虑兼容问题。

0.2 cmake版本

坑2:Ubuntu18 通过apt方式安装cmake的版本是 3.10.2。对grpc来说,太低了。所以我们直接安装目前的最高版本。
cmake3.20 目前是最新版本,方法如下
坑3: 如果wget慢的像乌龟,那么可以考虑用windows迅雷下载,然后导入linux系统中。如果系统中低版本的cmake,先卸载掉

wget https://cmake.org/files/v3.20/cmake-3.20.0-rc1-linux-x86_64.sh
cmake-3.20.0-rc1-linux-x86_64.sh --prefix=/usr/local --exclude-subdir
0.3 安装zlib

方法如下

wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar -xvf zlib-1.2.11.tar.gz
cd zlib-1.2.11/
./configure
make
sudo make install

这一步如果不安装后面make编译会报zlib.h找不到

1. 复制代码库

坑4:直接从github上拉仓库会奇慢无比,最好用gitee倒一下。自己倒腾也行,用下面我的gitee也行。见本文最后.gitmodules 。

2. 拉取grpc ,指定版本v1.12.0

坑5:新版本我试了好几次,make那一步总出错,而且越补越漏。

git clone https://gitee.com/niubucai/grpc.git
cd grpc/ 
git tag 
git checkout v1.12.0
git submodule sync 
git submodule update --init
3. 单独安装protoc

将protoc更新到v3.6.1 make -j4是多和编译,-j后面的参数根据当前系统配置加快编译

cd third_party/protobuf
git tag
git checkout v3.6.1
./autogen.sh 
./configure
sudo make -j4
sudo make install
sudo ldconfig     
protoc --version  
4. 编译安装grpc 加参数 HAS_SYSTEM_PROTOBUF=false
cd ../../ # 这里是grpc的目录
make HAS_SYSTEM_PROTOBUF=false 
sudo make install 

注意修改下.gitmodules文件,内容如下,如果自己从GitHub上复制了这些库,也可以用自己的拷贝。

5. 测试helloworld
终端1:
ubuntu@VM-0-16-ubuntu:~/grpc$ cd examples/cpp/helloworld/
ubuntu@VM-0-16-ubuntu:~/grpc/examples/cpp/helloworld$ ./greeter_server 
Server listening on 0.0.0.0:50051
终端2:
ubuntu@VM-0-16-ubuntu:~/grpc/examples/cpp/helloworld$ ./greeter_client 
Greeter received: Hello world
附录

.gitmodules 文件

[submodule "third_party/zlib"]
        path = third_party/zlib
        url = https://gitee.com/niubucai/zlib.git
        # When using CMake to build, the zlib submodule ends up with a
        # generated file that makes Git consider the submodule dirty. This
        # state can be ignored for day-to-day development on gRPC.
        ignore = dirty
[submodule "third_party/protobuf"]
        path = third_party/protobuf
        url = https://gitee.com/niubucai/protobuf.git
[submodule "third_party/googletest"]
        path = third_party/googletest
        url = https://gitee.com/niubucai/googletest.git
[submodule "third_party/benchmark"]
        path = third_party/benchmark
        url = https://gitee.com/niubucai/benchmark
[submodule "third_party/boringssl"]
        path = third_party/boringssl
        url = https://gitee.com/niubucai/boringssl.git
[submodule "third_party/boringssl-with-bazel"]
        path = third_party/boringssl-with-bazel
        url = https://gitee.com/niubucai/boringssl.git
[submodule "third_party/re2"]
        path = third_party/re2
        url = https://gitee.com/niubucai/re2.git
[submodule "third_party/cares/cares"]
        path = third_party/cares/cares
        url = https://gitee.com/niubucai/c-ares.git
        branch = cares-1_12_0
[submodule "third_party/bloaty"]
        path = third_party/bloaty
        url = https://gitee.com/niubucai/bloaty.git
[submodule "third_party/abseil-cpp"]
        path = third_party/abseil-cpp
        url = https://gitee.com/niubucai/abseil-cpp.git
        branch = lts_2020_02_25
[submodule "third_party/envoy-api"]
        path = third_party/envoy-api
        url = https://gitee.com/niubucai/data-plane-api.git
[submodule "third_party/googleapis"]
        path = third_party/googleapis
        url = https://gitee.com/niubucai/googleapis.git
[submodule "third_party/protoc-gen-validate"]
        path = third_party/protoc-gen-validate
        url = https://gitee.com/niubucai/protoc-gen-validate.git
[submodule "third_party/udpa"]
        path = third_party/udpa
        url = https://gitee.com/niubucai/udpa.git
[submodule "third_party/libuv"]
        path = third_party/libuv
        url = https://gitee.com/niubucai/libuv.git
[submodule "third_party/opencensus-proto"]
        path = third_party/opencensus-proto
        url = https://gitee.com/niubucai/opencensus-proto.git
[submodule "third_party/gflags"]
        path = third_party/gflags
        url = https://gitee.com/niubucai/gflags.git

文章参考
[1] https://blog.csdn.net/qq_32460819/article/details/114708740
[2] https://grpc.io/docs/guides/
[3] https://grpc.io/docs/languages/cpp/quickstart/

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值