源码安装GRPC && QA
1. cmake3 安装
wget https://github.com/Kitware/CMake/releases/download/v3.17.0/cmake-3.17.0.tar.gz
tar -zxf cmake-3.17.0.tar.gz
cd cmake-3.17.0
./bootstrap --prefix=/usr
make -j 8
sudo make install
cmake --version
2. 安装前置依赖
yum -y install devtoolset-8-gcc-c++.x86_64 devtoolset-8-gcc.x86_64 autoconf automake libtool make g++ unzip libgflags-dev libgtest-dev clang glibc libc++-dev openssl openssl-devel
3. 源码编译
- clone官方代码(我连了代理)
git clone https://github.com/grpc/grpc.git
cd grpc
git checkout v1.35.0
git submodule update --init //更新第三方源码
- 编译安装protobuf:
cd third_party/protobuf/
git submodule update --init --recursive
mkdir -p cmake/build
pushd cmake/build
source /opt/rh/devtoolset-8/enable
./autogen.sh //生成配置脚本
./configure --prefix=/usr/local/protobuf
make -j 8 && sudo make install
- 编译安装grpc
cd grpc
mkdir -p cmake/build
pushd cmake/build
source /opt/rh/devtoolset-8/enable
cmake3 -DCMAKE_INSTALL_PREFIX=/usr/local/grpc \
-DCMAKE_BUILD_TYPE=Release \
-DgRPC_INSTALL=ON \
-DBUILD_SHARED_LIBS=ON \
-DgRPC_BUILD_TESTS=OFF \
-DgRPC_ZLIB_PROVIDER=package \
-DgRPC_PROTOBUF_PROVIDER=package \
-DgRPC_SSL_PROVIDER=package \
-DProtobuf_INCLUDE_DIR=/usr/local/protobuf/include \
-DProtobuf_LIBRARY=/usr/local/protobuf/lib/libprotobuf.so \
-DProtobuf_PROTOC_LIBRARY=/usr/local/protobuf/lib/libprotoc.so \
-DProtobuf_PROTOC_EXECUTABLE=/usr/local/protobuf/bin/protoc ../..
make -j 8 && sudo make install
QA:
- cmake3 版本过低导致GRPC lib库无法正常安装
CMake Warning at cmake/abseil-cpp.cmake:33 (message):
gRPC_INSTALL will be forced to FALSE because gRPC_ABSL_PROVIDER is "module"
and CMake version (3.11.2) is less than 3.13.
Call Stack (most recent call first):
CMakeLists.txt:250 (include)
安装cmake3 版本大于3.11 见cmake安装
- Could NOT find Protobuf (missing: Protobuf_INCLUDE_DIR)
Protobuf_PROTOC_EXECUTABLE-NOTFOUND
CMake Error at /usr/share/cmake3/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
Could NOT find Protobuf (missing: Protobuf_INCLUDE_DIR)
Call Stack (most recent call first):
/usr/share/cmake3/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake3/Modules/FindProtobuf.cmake:543 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
cmake/protobuf.cmake:58 (find_package)
CMakeLists.txt:254 (include)
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
Protobuf_LIBRARY
linked by target "grpc_ruby_plugin" in directory /tmp/grpc
linked by target "grpc_php_plugin" in directory /tmp/grpc
linked by target "grpc_objective_c_plugin" in directory /tmp/grpc
linked by target "grpc_node_plugin" in directory /tmp/grpc
linked by target "grpc_csharp_plugin" in directory /tmp/grpc
linked by target "grpc++_reflection" in directory /tmp/grpc
linked by target "grpcpp_channelz" in directory /tmp/grpc
linked by target "grpc_python_plugin" in directory /tmp/grpc
linked by target "grpc++" in directory /tmp/grpc
linked by target "grpc++_alts" in directory /tmp/grpc
linked by target "grpc++_unsecure" in directory /tmp/grpc
linked by target "grpc_plugin_support" in directory /tmp/grpc
linked by target "gen_hpack_tables" in directory /tmp/grpc
linked by target "gen_legal_metadata_characters" in directory /tmp/grpc
linked by target "grpc++_error_details" in directory /tmp/grpc
linked by target "gen_percent_encoding_tables" in directory /tmp/grpc
linked by target "grpc_cpp_plugin" in directory /tmp/grpc
Protobuf_PROTOC_LIBRARY
linked by target "grpc_ruby_plugin" in directory /tmp/grpc
linked by target "grpc_php_plugin" in directory /tmp/grpc
linked by target "grpc_objective_c_plugin" in directory /tmp/grpc
linked by target "grpc_node_plugin" in directory /tmp/grpc
linked by target "grpc_csharp_plugin" in directory /tmp/grpc
linked by target "grpc_python_plugin" in directory /tmp/grpc
linked by target "grpc_plugin_support" in directory /tmp/grpc
linked by target "grpc_cpp_plugin" in directory /tmp/grpc
make[2]: *** No rule to make target `../../Protobuf_PROTOC_EXECUTABLE-NOTFOUND', needed by `gens/src/proto/grpc/channelz/channelz.grpc.pb.cc'. Stop.
make[2]: *** No rule to make target `../../Protobuf_PROTOC_EXECUTABLE-NOTFOUND', needed by `gens/src/proto/grpc/reflection/v1alpha/reflection.grpc.pb.cc'. Stop.
make[1]: *** [CMakeFiles/grpcpp_channelz.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/grpc++_reflection.dir/all] Error 2
make[2]: *** No rule to make target `../../Protobuf_PROTOC_EXECUTABLE-NOTFOUND', needed by `gens/src/proto/grpc/status/status.grpc.pb.cc'. Stop.
make[1]: *** [CMakeFiles/grpc++_error_details.dir/all] Error 2
问题原因:
- 源码编译protobuf时,指定了其它安装路径,导致编译grpc时无法找到protobuf库:
cd third_party/protobuf/
git submodule update --init --recursive
./autogen.sh
./configure --prefix=/usr/local/protobuf -->指定了安装路径,导致grpc编译时找不到protobuf相关路径
make -j 8 && sudo make install
解决办法:
- cmake 编译grpc时增加编译参数指定路径:
-DProtobuf_INCLUDE_DIR: protobuf include路径->安装路径/include (/usr/local/protobuf/include)
-DProtobuf_LIBRARY:libprotobuf.so路径->安装路径/lib/libprotobuf.so (/usr/local/protobuf/lib/libprotobuf.so)
macOS 对应libprotobuf.dylib
-DProtobuf_PROTOC_LIBRARY:libprotoc.so路径->安装路径/lib/libprotoc.so (/usr/local/protobuf/lib/libprotoc.so)
macOS 对应 libprotoc.dylib
-DProtobuf_PROTOC_EXECUTABLE:protoc路径->安装路径/bin/protoc (/usr/local/protobuf/bin/protoc)
cmake3 -DCMAKE_INSTALL_PREFIX=/usr/local/grpc \
-DCMAKE_BUILD_TYPE=Release \
-DgRPC_INSTALL=ON \
-DBUILD_SHARED_LIBS=ON \
-DgRPC_BUILD_TESTS=OFF \
-DgRPC_ZLIB_PROVIDER=package \
-DgRPC_PROTOBUF_PROVIDER=package \
-DgRPC_SSL_PROVIDER=package \
-DProtobuf_INCLUDE_DIR=/usr/local/protobuf/include \
-DProtobuf_LIBRARY=/usr/local/protobuf/lib/libprotobuf.so \
-DProtobuf_PROTOC_LIBRARY=/usr/local/protobuf/lib/libprotoc.so \
-DProtobuf_PROTOC_EXECUTABLE=/usr/local/protobuf/bin/protoc ../..
- openresty lua程序ffi.load 无法找到libxxx.so(libprotobuf.so/libgrpcXX.so):
解决办法:
/etc/ld.so.conf.d 目录下新增xxx.conf文件
内容加入protobuf和grpc的lib库目录:
/usr/local/protobuf/lib
/usr/local/grpc/lib
/usr/local/grpc/lib64
保存后执行命令: ldconfig