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

配置 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})

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"
    }
  }
}
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android Studio中编写C/C++代码,你需要进行以下步骤: 1. 创建一个新的Android Studio项目。 2. 在你的项目中创建一个新的C/C++源文件。你可以使用菜单File->New->C/C++ Source File来创建源文件。 3. 在你的源文件中编写C/C++代码。 4. 在你的项目中创建一个新的JNI头文件。你可以使用菜单File->New->JNI/C++ Header File来创建头文件。 5. 在你的JNI头文件中声明你的C/C++函数和变量。例如,如果你的C/C++源文件中有一个函数叫做myFunction,你可以在你的JNI头文件中这样声明它: ```c++ JNIEXPORT void JNICALL Java_com_example_myapp_MyClass_myFunction(JNIEnv *env, jobject obj); ``` 请注意,这里的Java_com_example_myapp_MyClass_myFunction是根据你的Java类和函数名称来自动生成的。你需要根据你的实际项目进行修改。 6. 在你的Java代码中,使用JNI接口调用你的C/C++函数。例如,如果你的Java类是MyClass,你可以在它的某个方法中这样调用你的C/C++函数: ```java public void myMethod() { myFunction(); } ``` 7. 在你的项目中配置NDK环境。你需要下载NDK并在你的项目中配置NDK路径。你可以使用菜单File->Project Structure->SDK Location来配置NDK路径。 8. 在你的项目中配置CMake。你需要创建一个CMakeLists.txt文件来告诉Android Studio如何编译你的C/C++代码。你可以使用以下模板来创建CMakeLists.txt文件: ``` cmake_minimum_required(VERSION 3.10.2) project(myproject C CXX) add_library(mylib SHARED mysource.cpp) target_link_libraries(mylib log) ``` 请注意,这里的mysource.cpp和mylib是根据你的实际项目进行修改。 9. 运行你的项目并测试你的C/C++代码。 请注意,以上步骤只是一个基本的示例,你需要根据你的实际项目需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值