The method of C++ code cross-compilation and exception location based on CMake on x86linux server

1. Compile the binary file of the arm architecture

1. Download ndk, since our server is based on linux environment, so we download android-ndk-r21e-linux-x86_64.zip, and then decompress it to the corresponding directory

Note: The version of ndk must be matched, if the version is inconsistent, an error may be reported when compiling

2. Android CMake, download any version directly from the official website, install it, check whether the installation is successful, you can verify it by command cmake -version, if you can check the version, it means the installation is successful

3. Write a CMakeList file for compiling C/C++ code, which can be configured to compile dynamic libraries, static libraries, and executable files. According to needs, you can configure them normally

4. Write a shell script and create a new run.sh script. In the shell script, specify the path of the ndk environment, the target architecture DANDROID_ABI to be compiled, and the Android target version DANDROID_NATIVE_API_LEVEL. The script will automatically find the CMakeList file in step 3 to compile. Execute the file, run.sh code is as follows:

mkdir -p build

cd build
rm -rf *

cmake .. \
-DCMAKE_TOOLCHAIN_FILE=/xxxxx/android-ndk-r21e/build/cmake/android.toolchain.cmake \
-DANDROID_ABI="arm64-v8a" \
-DANDROID_NATIVE_API_LEVEL=android-29
make -j4
echo "编译时间:"  $(date +%T)

#DCMAKE_TOOLCHAIN_FILE为自己的ndk路径
#DANDROID_AB 可根据需要去配置不同的ABI,即cpu架构

5. After writing the CMakeList file, run run.sh to compile the ABI executable file you want

2. Abnormal location method

Another advantage of cross-compiling in the ndk environment is that it can locate program exceptions. When a program exception or error occurs in a binary file compiled by C/C++, it generally only prompts a memory address like 0000000000084b6c, and does not clearly display the specific address. One line of code reports an error, which will make it difficult to locate the problem; you can locate the specific code error location according to addr2line in the ndk tool chain. The example is as follows: 1 is the path to configure the ndk environment addr2line for yourself, and 2 is the path to the binary file

Command:android-ndk-r21e/toolchains/aarch64-linux-android-4.9/prebuilt/linux-x86_64/bin/aarch64-linux-android-addr2line -f -C -e xxxx/bin/AC_card_agent 0000000000084b6c  

3. Answers to some questions

1. How to check whether your server belongs to x86 or arm64 architecture

Can be viewed through the uname -a command

The X86 server is as follows:

 The arm server is as follows:

 2. How to check whether the compiled binary executable file is x86 or arm64

It can be viewed through the file + executable file name command. Examples are as follows:

 3. Why can't the compiled executable file run?

This situation is generally caused by the inconsistency between the architecture of the executable file and the architecture of the operating environment. You can check whether they are consistent through the above two steps

Guess you like

Origin blog.csdn.net/banzhuantuqiang/article/details/131420493