因为接触了一些第三方项目和其他原因,我需要了解一些 RPC 相关的知识,首选的就是 Google 的 gRPC 库了。
安装
依然是使用WSL,发行版为Ubuntu 22.04.5 LTS
gRPC的官网如下:https://grpc.org.cn/docs/languages/cpp/quickstart/
他们的官网只建议通过下载项目编译的方式进行安装,我不是很喜欢这种方式,但是也只能按照教程来了。
构建并本地安装 gRPC 和 Protocol Buffers
设置
选择一个目录来存放本地安装的软件包。
export MY_INSTALL_DIR=$HOME/.local
确保目录存在
mkdir -p $MY_INSTALL_DIR
将本地 bin 文件夹添加到 path 变量中
export PATH="$MY_INSTALL_DIR/bin:$PATH"
安装 cmake
我的发行版使用如下cmake
:
cmake version 3.22.1CMake suite maintained and supported by Kitware (kitware.com/cmake).
你没安装可以装一个:
sudo apt install -y cmake
安装其他必需工具
安装构建 gRPC 所需的基本工具
sudo apt install -y build-essential autoconf libtool pkg-config
克隆 grpc 仓库
克隆 grpc 仓库及其子模块。这一步是我觉得最操蛋的。
首先如果你网络条件不错,那可以用官方的 github 地址:
git clone --recurse-submodules -b v1.71.0 --depth 1 --shallow-submodules https://github.com/grpc/grpc
不太行就用国内的地址:
git clone --recurse-submodules -b v1.71.0 --depth 1 --shallow-submodules https://gitee.com/mirrors/grpc
但是由于 grpc 用了太多第三方库,第三方库自动从 github 下载,所以这些第三方库也有可能不会克隆成功。比如这样的提示代表着googletest
库没下载成功:
fatal: unable to access 'https://github.com/google/googletest.git/': GnuTLS recv error (-110): The TLS connection was non-properly terminated.
Unable to fetch in submodule path 'third_party/googletest'; trying to directly fetch 2dd1c131950043a8ad5ab0d2dda0e0970596586a:
fatal: unable to access 'https://github.com/google/googletest.git/': Failed to connect to github.com port 443 after 130117 ms: Connection timed out
fatal: Fetched in submodule path 'third_party/googletest', but it did not contain 2dd1c131950043a8ad5ab0d2dda0e0970596586a. Direct fetching of that commit failed.
你可能需要手动下载,放到grpc/third_party
下。并不是所有的第三方库没有都会构建失败的,可以查看他们 github 的目录确认:https://github.com/grpc/grpc/tree/master/third_party
构建并安装 gRPC 和 Protocol Buffers
虽然不是强制性的,但 gRPC 应用程序通常利用 Protocol Buffers 进行服务定义和数据序列化,并且示例代码使用了 proto3。
以下命令构建并本地安装 gRPC 和 Protocol Buffers:
cd grpc
mkdir -p cmake/build
pushd cmake/build
cmake -DgRPC_INSTALL=ON \-DgRPC_BUILD_TESTS=OFF \-DCMAKE_CXX_STANDARD=17 \-DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR \../..
make -j 4
make install
popd
这一步,之前如果某个重要的三方库没有下载成功,这里就会报错了。比如我的re2
库没下载成功,首先是构建的时候给出了警告:
CMake Warning at cmake/re2.cmake:41 (message):gRPC_RE2_PROVIDER is "module" butRE2_ROOT_DIR(/home/bluebonnet27/ThirdParty/grpc/third_party/re2) is wrong
Call Stack (most recent call first):CMakeLists.txt:384 (include)
构建时直接报错:
/home/bluebonnet27/ThirdParty/grpc/src/core/util/matchers.h:27:10: fatal error: re2/re2.h: No such file or directory27 | #include "re2/re2.h"| ^~~~~~~~~~~
compilation terminated.
这时候就得单独克隆缺少的re2
库。
一个没有克隆成功的库里只会有.git
文件,别的都没有。
bluebonnet27@bluebonnet27:~/ThirdParty/grpc/third_party/protobuf$ ls -a
. .. .git
克隆之前记得把本地目录先删了,否则会报目录已存在的错误。反正折腾到最后,我还是想办法改善了下我的网络环境,否则不知道要折腾多久。