C++ cross-compile grpc

Download source code

When downloading the source code, be sure to download the third-party library. How to download can refer to my previous article
"Compiling and installing grpc on windows using C++"

Install dependencies

install openssl

You can check to see if you have installed it. If openssl version
Insert image description here
not, install it:

//# Install openssl (to use instead of boringssl)
apt-get update && apt-get install -y libssl-dev

Install CMake 3.16

The version of cmake must be greater than version 3.16

apt-get update && apt-get install -y wget
wget -q -O cmake-linux.sh https://github.com/Kitware/CMake/releases/download/v3.16.1/cmake-3.16.1-Linux-x86_64.sh
sh cmake-linux.sh -- --skip-license --prefix=/usr
rm cmake-linux.sh

设置
GRPC_CPP_DISTRIBTEST_BUILD_COMPILER_JOBS=${GRPC_CPP_DISTRIBTEST_BUILD_COMPILER_JOBS:-4}

Build protoc

This must be built first, because we will use it later in the construction of grpc.

# Build and install gRPC for the host architecture.
# We do this because we need to be able to run protoc and grpc_cpp_plugin
# while cross-compiling.
//进入grpc的源代码文件夹
mkdir -p "cmake/build"			//新建一个文件夹,用来存储编译过程中中的二进制文件
pushd "cmake/build"				//进入文件夹
cmake \
  -DCMAKE_BUILD_TYPE=Release \	//选择release模式构建
  -DgRPC_INSTALL=ON \			//是否安装
  -DgRPC_BUILD_TESTS=OFF \		//是否构建测试
  -DgRPC_SSL_PROVIDER=package \	//
  ../..
make "-j${GRPC_CPP_DISTRIBTEST_BUILD_COMPILER_JOBS}" install

If there is a C++ compiler error when you build it yourself, it means there is a problem during multi-thread compilation, and we do not need the -j parameter.

Writing tool compilation files

cat > /tmp/toolchain.cmake <<'EOT'  						//在/tmp目录下新建一个文件toolchain.cmake
SET(CMAKE_SYSTEM_NAME Linux)								//设置系统为 linux
SET(CMAKE_SYSTEM_PROCESSOR aarch64)							//设置了目标程序的架构为arm64位
set(CMAKE_STAGING_PREFIX /tmp/stage)						//设置了安装路径		
set(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc-6)		//设置c编译器
set(CMAKE_CXX_COMPILER /usr/bin/aarch64-linux-gnu-g++-6)	//设置了c++编译器
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

Start compiling grpc

# Build and install gRPC for ARM.
# This build will use the host architecture copies of protoc and
# grpc_cpp_plugin that we built earlier because we installed them
# to a location in our PATH (/usr/local/bin).
mkdir -p "cmake/build_arm"								//新建文件夹
pushd "cmake/build_arm"
cmake -DCMAKE_TOOLCHAIN_FILE=/tmp/toolchain.cmake \		//设置使用的交叉编译的文件
      -DCMAKE_BUILD_TYPE=Release \						//设置编译的模式为release
      -DCMAKE_INSTALL_PREFIX=/tmp/install \
      ../..
make "-j${GRPC_CPP_DISTRIBTEST_BUILD_COMPILER_JOBS}" install

If a C++ compiler error occurs during the compilation process, cancel the -j parameter and just make.

Test and call grpc

# 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 "-j${GRPC_CPP_DISTRIBTEST_BUILD_COMPILER_JOBS}"

After the project is successfully built, you can run the server and client directly on arm.

mistake:

After compiling it myself and preparing to call it, an error occurred.
The version of libc.so.6 could not be found.
This is because the version and information of the system on my arm do not match, and the version of the cross-compiler I use is relatively new.
In this case, I could only replace the old cross-compiler. Instead of downloading it from the official website, I relied on the system's own installation.

32-bit cross-compilation tool chain

sudo apt-get install gcc-arm-linux-gnueabihf

After installation, you can use the compile command:arm-linux-gnueabihf-gcc

64-bit cross-compilation tool chain

sudo apt-get install gcc-aarch64-linux-gnu

After installation, you can use the compile command:aarch64-linux-gnu-gcc

Guess you like

Origin blog.csdn.net/simple_core/article/details/127408007