Summary of CMakeLists.text used in NDK development

When we create a project, if Include C++ Support is checked, a CMakeLists.text will be generated in the same level directory of main

Let's introduce one by one

# 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).
             src/main/cpp/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} )

We can translate it directly:

# For more information about using CMAGE with Android studio,
# Read the document: https://d.android.com/studio/projects/add-native-code.html

# Set the minimum CMake version required to create a local library

cmake_minimum_required(VERSION 3.4.1)

# Create and name the library, set it to STATIC or SHARED, 
# And provide the relative path of its source code.
# You can define multiple libraries, and CMake builds them for you.
# Use Apk to package shared library automatically

add_library( # Set the name of the library
             native-lib

             # Set the library as a shared library
             SHARED

             # Provides a relative path for the source file
             src/main/cpp/native-lib.cpp
             )

# Searches for the specified preconstruction library and stores the path as a variable.
# Because CMake contains the system library in the search path by default
# You only need to specify the name of the public NDK library to add
# CMake verifies that the library exists before completing the 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 that the library CMake should be linked to the target library
# You can link multiple libraries as defined in this build script,
# A pre built third-party library or system library

target_link_libraries( # Specifies the target library
                       native-lib

                       # Link target library to log Library
                       # Included in NDK
                       ${log-lib} )

Analysis:

1. Make the minimum version required by CMake, which is automatically generated by the system and does not need to be changed
    cmake_minimum_required(VERSION 3.4.1)

 

2. add_library(): used to set the properties related to compiling and generating the local library

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).
             src/main/cpp/native-lib.cpp
             )

native-lib :

Name of the build dynamic library

SHARE:

Set the type of link library, divided into: SHARE dynamic link library, SATTIC static link library

src/main/cpp/native-lib.cpp:

The relative path of your c/c + + code. At present, there is a file named natural-lib.cpp in my CPP directory. If I add another hello.cpp, I will add

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

             SHARED

             src/main/cpp/native-lib.cpp
             src/main/cpp/hello.cpp
             )

 

3. find_library(): used to add some dependency libraries for compiling local NDK libraries

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 )

Log lib: alias of dependent Library

log: log log Library

 

4. Target? Link? Libraries(): used to associate our local library with the library of a third party

target_link_libraries( # Specifies the target library.
                       native-lib

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

native-lib:

Our own library, which is the same as the name in add library

${log-lib}:

The name of the library that needs to be associated.

 

Note: Reference: https://blog.csdn.net/hktkfly6/article/details/79593127

Keywords: Mobile cmake Android Gradle

Added by cheald on Thu, 12 Dec 2019 17:18:39 +0200