编译、交叉编译 gRPC
心得体会:优先看官方教程,不要随便百度一篇 CSDN/博客园教程照着做!
如果你搜到这篇文章,而且还没看过 Google 官方的编译教程,建议先看看文章底部的参考链接
1 下载代码
git clone -b v1.48.x https://github.com/grpc/grpc --depth=1
cd grpc
git submodule update --init --depth=1
2 编译 host x86_64
mkdir -p cmake/build
pushd cmake/build
cmake \
-DCMAKE_BUILD_TYPE=Release \
-DgRPC_INSTALL=ON \
-DgRPC_BUILD_TESTS=OFF \
-DgRPC_SSL_PROVIDER=package \
../..
sudo make -j6 install
popd
3 设置交叉编译工具链
# Write a toolchain file to use for cross-compiling.
cat > /tmp/toolchain.cmake <<'EOT'
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_STAGING_PREFIX /tmp/stage)
# 以下两行根据所用的交叉编译器所在路径修改
set(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/aarch64-linux-gnu-g++)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
EOT
4 交叉编译
mkdir -p "cmake/build_arm"
pushd "cmake/build_arm"
cmake -DCMAKE_TOOLCHAIN_FILE=/tmp/toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/tmp/install \
../..
make -j6 install
popd
5 交叉编译 HelloWorld
# Build helloworld example for ARM.
# As above, it will find and use protoc and grpc_cpp_plugin
# for the host architecture.
mkdir -p "examples/cpp/helloworld/cmake/build_arm"
pushd "examples/cpp/helloworld/cmake/build_arm"
cmake -DCMAKE_TOOLCHAIN_FILE=/tmp/toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-Dabsl_DIR=/tmp/stage/lib/cmake/absl \
-DProtobuf_DIR=/tmp/stage/lib/cmake/protobuf \
-DgRPC_DIR=/tmp/stage/lib/cmake/grpc \
../..
make -j6
参考链接
- https://github.com/grpc/grpc/blob/master/BUILDING.md
- https://github.com/grpc/grpc/blob/master/test/distrib/cpp/run_distrib_test_cmake_aarch64_cross.sh