如上篇所说,使用NDK生成的独立工具链可以编译第三方库,生成独立工具链见google文档:独立工具链
下面展示了使用此脚本的示例:
$NDK/build/tools/make-standalone-toolchain.sh --arch=arm --platform=android-21 --install-dir=/tmp/my-android-toolchain
默认情况下,构建系统使用 32 位、基于 ARM 的 GCC 4.8 工具链。不过,您可以通过将 --arch=<toolchain> 指定为选项来指定一个不同的值。表 3 显示将用于其他工具链的值:
使用 --arch。
工具链值
mips64 编译器--arch=mips64
mips GCC 4.8 编译器--arch=mips
x86 GCC 4.8 编译器--arch=x86
x86_64 GCC 4.8 编译器--arch=x86_64
mips GCC 4.8 编译器--arch=mips
下面根据生成的独立工具链,来编译我们自己的C/C++代码并引入ndk编译后的第三方库。我们为工程写一个cmake,如下CMakeList.txt:
cmake_minimum_required(VERSION 3.5)
project(EXMAPLE)
set(CMAKE_C_STANDARD 99)
set(UPLOAD_ROOT ${PROJECT_SOURCE_DIR})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pie -fPIE")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pie -fPIE")
set(PLATFROM_BUILD_TYPE "Android")
set(TOOL_CHAIN_GCC_ROOT /tmp/ndk_tool)
message(STATUS "build ${PLATFORM} lib")
set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_AR ${TOOL_CHAIN_GCC_ROOT}/bin/arm-linux-androideabi-ar)
set(CMAKE_ASM_COMPILER ${TOOL_CHAIN_GCC_ROOT}/bin/arm-linux-androideabi-as)
set(CMAKE_RANLIB ${TOOL_CHAIN_GCC_ROOT}/bin/arm-linux-androideabi-ranlib)
set(CMAKE_C_COMPILER ${TOOL_CHAIN_GCC_ROOT}/bin/arm-linux-androideabi-clang)
set(CMAKE_CXX_COMPILER ${TOOL_CHAIN_GCC_ROOT}/bin/arm-linux-androideabi-clang++)
set(MDSPEECH_STRIP ${TOOL_CHAIN_GCC_ROOT}/bin/aarch64-openwrt-linux-gnu-strip)
set(CMAKE_FIND_ROOT_PATH ${TOOL_CHAIN_GCC_ROOT})
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)
message(STATUS "Build example demo...")
include_directories(./include ./include/libwebsockets ./include/mbedtls ./include/uv)
add_executable(example_demo ./src/network_lws.c)
target_link_libraries(example_demo ${UPLOAD_ROOT}/lib/libwebsockets.so
${UPLOAD_ROOT}/lib/libmbedtls.so
${UPLOAD_ROOT}/lib/libmbedcrypto.a
${UPLOAD_ROOT}/lib/libmbedx509.a
${UPLOAD_ROOT}/lib/libuv.so
libc.so)
message(STATUS "Build example demo done")