[14] Cmake series NDK Detailed CMakeLists.txt detailed configuration

1. CMake Introduction

CMake compilation is a more advanced configuration tools than make, it can be based on different platforms, different compiler to generate the corresponding Makefile or vcproj project. So as to achieve the purpose of cross-platform.

Android Studio is generated using the CMake ninja, ninja is a small concern the speed of the build system. We do not care about ninja script, know how to configure cmake it. Which can be seen cmake is actually a variety of tools to build a cross-platform support output of a different script.

In android studio 2.2 and above, the default tool to build the native library is CMake.

2. Basic Commands

#cmake最低版本
cmake_minimum_required(VERSION 3.6.0)

#指定项目
project(Main)

add_executable(demo demo.cpp) # 生成可执行文件
add_library(common STATIC util.cpp) # 生成静态库
add_library(common SHARED util.cpp) # 生成动态库或共享库

Search c / cpp files

# 搜索当前目录下的所有.cpp文件, 但不能查找子目录
aux_source_directory(. SRC_LIST) 
add_library(demo ${SRC_LIST})

#也可以 
file(GLOB DIR_SRCS *.c)
add_executable(main ${DIR_SRCS})

Use subdirectories CMakeList

# 添加 child 子目录下的cmakelist
add_subdirectory(child)
# 指定生成目标 
add_library(main ${DIR_SRCS})
# 添加链接库
target_link_libraries(main child)
//--------------------------------------
# child中设置
aux_source_directory(. DIR_LIB_SRCS)
# 生成链接库 默认生成静态库
add_library (child ${DIR_LIB_SRCS})
#指定编译为静态库
add_library (child STATIC ${DIR_LIB_SRCS})

Setting pre-compiled macro

#设置预编译宏 cflag和cxxflag
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DTEST"  )
set(CMAKE_Cxx_FLAGS "${CMAKE_Cxx_FLAGS} -DTEST"  )  

The introduction of header files

#这样就可以使用 #include <xx> 引入 
#否则需要使用 #include "path/xx" 
include_directories( imported-lib/include/ )

Analyzing logic statements

#逻辑判断 计较字符串
set(ANDROID_ABI "areambi-v7a")
if(${ANDROID_ABI} STREQUAL "areambi")
  	message("armv5")
elseif(${ANDROID_ABI} STREQUAL "areambi-v7a")
	message("armv7a")
else()
	
endif()

3. build.gradle Configuration

//还可以在gradle中使用 arguments  设置一些配置
externalNativeBuild {
      cmake {
        arguments "-DANDROID_TOOLCHAIN=clang",	//使用的编译器clang/gcc
                  "-DANDROID_STL=gnustl_static" //cmake默认就是 gnustl_static
        cFlags "" //这里也可以指定cflag和cxxflag,效果和之前的cmakelist里使用一样
        cppFlags "" 
      }
    }	

4. Notes

  1. Android.mk in use> = you can not use precompiled dynamic library (static library is no problem) on 6.0 devices:

    cmake_minimum_required(VERSION 3.4.1)
    
    file(GLOB SOURCE *.c )
    add_library(
                 hello-jni
                 SHARED
                ${SOURCE} )
                
    #这段配置在6.0依然没问题 
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -L[SO所在目录]")
    
    #这段配置只能在6.0以下使用 
    #add_library(Test SHARED IMPORTED)
    #set_target_properties(Test PROPERTIES IMPORTED_LOCATION [SO绝对地址])
    
    target_link_libraries(  hello-jni Test )
    
    1. There are two dynamic library libhello-jni.sowith libTest.so.

      libhello-jni.so 依赖于libtest.so (使用NDK下的ndk-depends` to view dependencies), then:

      //<=5.0:
      	System.loadLibrary("Test");
      	System.loadLibrary("hello-jni");
      //>=6.0:
      	System.loadLibrary("hello-jni");
      

Guess you like

Origin blog.csdn.net/u011077027/article/details/93240811