cmake链接ndk交叉编译

需求: 用CMake构建和编译生成的算法库, 作为动态共享库link到Android项目.

配置

在Android项目的Android.mk中添加:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
include $(CLEAR_VARS)
LOCAL_PATH          := $(XXXX_PATH)
LOCAL_MODULE        := libxxx
LOCAL_MULTILIB      := 64
LOCAL_SRC_FILES_64  := ./algo/libxxx.so
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE_TAGS   := optional
LOCAL_MODULE_CLASS  := SHARED_LIBRARIES
LOCAL_PROPRIETARY_MODULE := true
include $(BUILD_PREBUILT)

include $(CLEAR_VARS)

算法库的CMakeLists.txt中添加一下编译项和宏:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
CMAKE_MINIMUM_REQUIRED(VERSION 3.5)

ADD_DEFINITIONS("-Wall")
ADD_DEFINITIONS("-fPIC")
ADD_DEFINITIONS("-Wl,-lm")
ADD_DEFINITIONS("-Wl,--whole-archive")
SET(CMAKE_CXX_FLAGS "-std=c++17 -pthread -Wall -pie -fPIC -Wl,-Bsymbolic -lz -lc -ldl -lm -D__STDINT_LIMITS -D__STDINT_MACROS -D__ANDROID__ -DANDROID")
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR})

ADD_EXECUTABLE(xxx xxx.cpp)

算法库的build.sh编译脚本如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
function build() {
    export ANDROID_NDK=$HOME/android-ndk-r21e
    export PATH=$ANDROID_NDK:$PATH

    rm -r .build
    mkdir .build && cd .build

    cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
        -DANDROID_ABI=arm64-v8a \
        -DANDROID_NDK=$ANDROID_NDK \
        -DANDROID_PLATFORM=latest \
        -DANDROID_LD=lld \
        ..

    make -j12 && make

    cd ..
    rm -r .build/
}
build

需要注意的是:

  1. 设置环境变量, 添加ndk路径到环境变量
  2. cmake配置参数, 按照如上填写
  3. DANDROID_ABI要与link库索声明的类型匹配, 比如Android.mk中link库声明的是64位, 则这里填写arm64-v8a

链接

  1. ndk下载
  2. cmake和ndk交叉编译