NDK工具包下载
NDK 下载 | Android NDK | Android Developers
https://github.com/android/ndk/wiki/Unsupported-Downloads
以android-ndk-r26c下载为例,下载后将压缩包解压至/usr目录下
CMakeLists编译选项设置
编译平台变量判断条件中增加一下android条件下CMake的变量
CMAKE_MINIMUM_REQUIRED(VERSION 3.10)
if(BUILD_TARGET_PLATFORM STREQUAL "aarch64")
message(STATUS "BUILD_TARGET_PLATFORM: " "${BUILD_TARGET_PLATFORM}")
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(CMAKE_C_COMPILER /usr/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER /usr/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-g++)
elseif(BUILD_TARGET_PLATFORM STREQUAL "android")
message(STATUS "BUILD_TARGET_PLATFORM: " "${BUILD_TARGET_PLATFORM}")
set(CMAKE_SYSTEM_PROCESSOR aarch64)
set(ANDROID_NDK /usr/android-ndk-r26c)
set(CMAKE_TOOLCHAIN_FILE ${ANDROID_NDK}/build/cmake/android.toolchain.cmake)
set(ANDROID_ABI arm64-v8a)
set(ANDROID_PLATFORM android-28)
set(ANDROID_STL c++_static)
set(ANDROID_LD default)
message(STATUS "BUILD_TARGET_PLATFORM: " "${BUILD_TARGET_PLATFORM}")
endif()
project(LINGJING_NHCB LANGUAGES C CXX)
详细变量设置参考
C++ 库支持 | Android NDK | Android Developers
编译出的程序默认链接器为/system/bin/linker64表示成功
注意事项:
https://developer.android.google.cn/ndk/guides/cpp-support?hl=zh-cn
NDK对LINUX下的C库进行了重构和改造形成了属于NDK自己的c库,该库集合GNU的pthread等常见库于一体,因此LINUX平台下的一些代码直接编译可能会报错,如多线程的一些宏定义等。需要手动逐个进行修改。