1. 子模塊1:gmath靜態庫CMakeLists.txt
#要求的cmake最小版本
cmake_minimum_required(VERSION 3.4.1)
#打開cmake輸出日志
set(CMAKE_VERBOSE_MAKEFILE on)
#添加靜態庫,庫名稱gmath
add_library(gmath STATIC src/gmath.c)
#定義變量distribution_DIR, ${CMAKE_CURRENT_SOURCE_DIR}表示當前文件所在的目錄
set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../distribution)
#設置靜態庫gmath的輸出目錄
set_target_properties(gmath
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY #靜態庫輸出目錄
"${distribution_DIR}/gmath/lib/${ANDROID_ABI}")
# 添加自定義命令,拷貝頭文件"${CMAKE_CURRENT_SOURCE_DIR}/src/gmath.h"到"${distribution_DIR}/gmath/include/gmath.h"
add_custom_command(TARGET gmath POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E
copy "${CMAKE_CURRENT_SOURCE_DIR}/src/gmath.h"
"${distribution_DIR}/gmath/include/gmath.h"
COMMENT "Copying gmath to output directory")
2. 子模塊2:gpref動態庫CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
#添加動態庫,庫名稱gpref
add_library(gperf SHARED src/gperf.c)
#定義變量distribution_DIR
set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../../distribution)
#設置動態庫gpref的輸出目錄
set_target_properties(gperf
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY #動態庫輸出目錄
"${distribution_DIR}/gperf/lib/${ANDROID_ABI}")
#添加自定義命令,拷貝頭文件
add_custom_command(TARGET gperf POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E
copy "${CMAKE_CURRENT_SOURCE_DIR}/src/gperf.h"
"${distribution_DIR}/gperf/include/gperf.h"
COMMENT "Copying gperf to output directory")
3. 主模塊:CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(lib_src_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(lib_build_DIR $ENV{HOME}/tmp/${ANDROID_ABI})
file(MAKE_DIRECTORY ${lib_build_DIR}) #MAKE_DIRECTORY表示創建目錄
#添加子模塊,參數1:src目錄,參數:bin目錄
add_subdirectory(${lib_src_DIR}/gmath ${lib_build_DIR}/gmath)
add_subdirectory(${lib_src_DIR}/gperf ${lib_build_DIR}/gperf)
4. 主工程引入使用gmath靜態庫和gpref動態庫:CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../distribution)
#以導入方式添加靜態庫
add_library(lib_gmath STATIC IMPORTED)
#指定導入的庫位置
set_target_properties(lib_gmath PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/gmath/lib/${ANDROID_ABI}/libgmath.a)
#以導入方式添加動態庫
add_library(lib_gperf SHARED IMPORTED)
#指定導入的庫位置
set_target_properties(lib_gperf PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/gperf/lib/${ANDROID_ABI}/libgperf.so)
#通c++ flags指定使用的c++標准庫為gnu++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
#添加動態庫,庫名稱hello-libs
add_library(hello-libs SHARED hello-libs.cpp)
#指定頭文件路徑
target_include_directories(hello-libs PRIVATE
${distribution_DIR}/gmath/include
${distribution_DIR}/gperf/include)
#鏈接hello-libs到android、lib_gmath、lib_gperf、log。其中android和log是系統自動的庫。
target_link_libraries(hello-libs
android
lib_gmath
lib_gperf
log)
5. build.gradle的配置
defaultConfig {
externalNativeBuild {
cmake {//cmake編譯方式
arguments '-DANDROID_STL=c++_static' //通過宏指定使用的c++模板庫為c++_static
}
}
}
externalNativeBuild {
cmake {
version '3.10.2' //cmake版本號
path 'src/main/cpp/CMakeLists.txt' //CMakeLists.txt文件路徑
}
}
Cmake官方文檔:https://cmake.org/documentation/ 結合Quick Search快速搜索學習很方便