子模块1:gmath静态库CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#要求的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:gpref动态库CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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")

主模块:CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
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)

主工程引入使用gmath静态库和gpref动态库:CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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)

# 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快速搜索学习很方便

参考链接:http://t.zoukankan.com/yongfengnice-p-13695598.html