当前位置:   article > 正文

【Android】使用 CMake 配置 C/C++ 代码的编译_android cmake配置

android cmake配置

配置 CMake

1、 创建 CMake 构建脚本

CMake 构建脚本是一个纯文本文件,您必须将其命名为 CMakeLists.txt。

注意:您可以在所需的任何位置创建构建脚本。不过,在配置 build 脚本时,原生源代码文件和库的路径将与 build 脚本的位置相关。

推荐位置:src/main/cpp/CMakeLists.txt
在这里插入图片描述

2、编写脚本内容:
# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.18.1)

# Declares and names the project.

project("MyNatives")

# 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.
        ${PROJECT_NAME}

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        MyNatives.cpp Calculate.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.
        ${PROJECT_NAME}

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

  • 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
  • 48

2、关联 Gradle

方式1:使用 Android Studio 界面

1、右键点击您想要关联到原生库的模块(例如 app 模块),然后从菜单中选择 Add C++ to Module,如下图所示:在这里插入图片描述
2、然后您将会看到类似于下图的对话框:在这里插入图片描述
3、如果选择的 module 已有 CMake 配置请选择第二项(Link an existing Cmakelists.txt…)并指定文件位置 ,反之选择第一项(Create CMakeListes.txt…),然后点击 OK

方式2:手动配置 Gradle

如需手动配置 Gradle 以关联到您的原生库,您需要将 externalNativeBuild 块添加到模块级 build.gradle 文件中,并使用 cmake 或 ndkBuild 块对其进行配置:

android {
  ...
  defaultConfig {...}
  buildTypes {...}

  // Encapsulates your external native build configurations.
  externalNativeBuild {

    // Encapsulates your CMake build configurations.
    cmake {

      // Provides a relative path to your CMake build script.
      path "src/main/cpp/CMakeLists.txt"
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/625223
推荐阅读
相关标签
  

闽ICP备14008679号