首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android NDK编程(二)---CMakeList.txt详解

Android NDK编程(二)---CMakeList.txt详解

作者头像
Vaccae
发布2019-07-25 14:40:23
3K0
发布2019-07-25 14:40:23
举报
文章被收录于专栏:微卡智享微卡智享

前言

前一篇我们介绍了《Android NDK编程(一)---NDK介绍及环境搭建》,简单介绍了一下什么是NDK和JNI,以前NDK环境的配置及怎么创建第一个NDK的程序,在上一篇的左侧栏中有一个CMakeList.txt的文件,这一章我们就来解析一下CMakeList文件。

CMake简介

CMake是一个跨平台的构建工具,可以用简单的语句来描述所有平台的安装(编译过程)。能够输出各种各样的makefile或者project文件。Cmake 并不直接建构出最终的软件,而是产生其他工具的脚本(如Makefile ),然后再依这个工具的构建方式使用。

CMake是一个比make更高级的编译配置工具,它可以根据不同平台、不同的编译器,生成相应的Makefile或者vcproj项目。从而达到跨平台的目的。Android Studio利用 CMake生成的是ninja,ninja是一个小型的关注速度的构建系统。我们不需要关心ninja的脚本,知道怎么配置cmake就可以了。从而可以看出cmake其实是一个跨平台的支持产出各种不同的构建脚本的一个工具。

CMake的脚本名默认是CMakeLists.txt。

CmakeList.txt

#cmake的最低版本
cmake_minimum_required(VERSION 3.6)

# ${ANDROID_ABI} 构建的COU
# ${CMAKE_SOURCE_DIR} cmakeList.txt所在的文件夹路径
# message("","") 相当于log
# add_subdirectory(路径)  引入路径下的 cmakeList
# 源文件 最终生成的 so
add_library(变量名A SHARED B.c)
add_library(变量名A STATIC B.c)

# 源文件 最终生成的 so   多文件设置
file(GLOB 变量名SRCC  src/main/cpp/*.c  src/main/cpp/*.cpp)
add_library(变量名A SHARED ${SRCC})

# =========================添加依赖库1=================================
# 设置静态库以导入的形式加入到项目中来
add_library(变量名B STATIC IMPORTED)
set_target_properties(变量名B PROPERTIES IMPORTED_LOCATION 路径1/xx.a  路径2/xxx.so)

# ========================= 添加依赖库2 =================================

#设置动态依赖库1
# 添加动态库,为了android版本之间的兼容性,需要设置一个变量
# CMAKE_CXX_FLAGS c++的参数 会传给编译器
# CMAKE_C_FLAGS c的参数 会传给编译器
# 重新定义 CMAKE_CXX_FLAGS 变量的内容   (如果是c文件,需要用 CMAKE_C_FLAGS )
# CMAKE_SOURCE_DIR 当前文件的地址
# ANDROID_ABI 当前手机的ABI
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}  -L${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}")

#设置动态依赖库2   会从 ndk中的platforms/android-21/arch-arm/usr/lib/  中查找log库 并赋值给log_lib
find_library( log-lib log )

# 为了确保 CMake 可以在编译时定位头文件 
# 这样就可以使用 #include <xx.h> 引入 
# 否则需要使用 #include "path/xx"
 include_directories( imported-lib/include/ )

# 设置依赖的库
# 如果是静态库,需要根据 add_library中设置的变量名B去查找
# 如果是动态库,需要根据  CMAKE_CXX_FLAGS 改变后的路径下去按照库名查找
target_link_libraries( 要生成的库A  依赖的库B  ${log-lib} )

我们默认生成的CMakeList.txt为下面代码

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

build.gradle

回到我们创建的那个vaccaendk的项目中,我们点击build.gradle的文件,特别关注一下android.defaultConfig.externalNativeBuild和externalNativeBuild这两个

android.defaultConfig.externalNativeBuild

externalNativeBuild { 
  cmake {arguments "-DANDROID_TOOLCHAIN=clang", //使用的编译器clang/gcc
                    "-DANDROID_STL=gnustl_static"//cmake默认就是 gnustl_static
              cFlags ""     //这里也可以指定cflag和cxxflag,效果和之前的cmakelist里使用一样
              cppFlags ""
             //指定需要编译的cpu架构
              abiFilters "armeabi-v7a"
             //指定需要编译的模块(如Cmake中配置了生成native-lib、native-lib2)
             //默认按照cmake的配置编译所有
             //这里指定只编译 native-lib 模块
            targets "native-lib"
  }

}

externalNativeBuild

externalNativeBuild {
            cmake {
              //指定脚本路径 当前build.gradle文件的相对路径
              path "xxx/CMakeLists.txt"
            }
}

-END-

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-04-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微卡智享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • CmakeList.txt
  • build.gradle
    • android.defaultConfig.externalNativeBuild
      • externalNativeBuild
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档