android CMakeLists.txt文件详解

8 篇文章 0 订阅
1 篇文章 0 订阅

很多时候时候我们不得不感叹工具的强大是我们的生活和和工作变的越来越简单。在android 的ndk 开发中我们历经了有命令编译到eclipse 在studio 的工具的变迁,这一切都是朝着开发流程越来越简单,开发效率越来越高的方向发展。
今天学习了如何在Studio 使用CMakeLists.txt配置文件进行NDK 开发,感觉流程简单了很多,今天想详细介绍换一下CMakeLists.txt的基本使用。因为在实际的开发中对应JNI的使用不会简单的时候一个c/c++文件,有时候会用到多个c/c++ 文件,甚至会生成多个so文件。所以我要分析一下
CMakeLists.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) 设置cmake最低版本

# 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.  设置生成so文件名
             hello

             # Sets the library as a shared library. 设置库的类型,一种是静态的STATIC .a文件,一种是动态SHARED so文件
             SHARED

             # Provides a relative path to your source file(s). 要编译的C/C++文件,我这里编译的是两个文件
             src/main/cpp/native-lib.cpp
             src/main/cpp/hello.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. 指定连接的目标库
                       hello
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

上面对CMakeLists文件中的关键代码做了简单注释。
如何生成多个so文件
方法一:
直接在app/下的CMakeLists.text 的末尾添加要生成的so 文件名,资源路径,添加内容如下

add_library(test-lib SHARED src/main/cpp/test.cpp)

target_link_libraries(test-lib ${log-lib} )

方法二 创建一个新的CMakeLists.txt文件
文件内容如下 和上面的内容是一样的,然后在app下的CMakeLists.txt 末尾添加

ADD_SUBDIRECTORY(src/main/cpp/one) 括号内的内容是我们创建的CMakeLists.txt的路径

好了,今天先介绍到这里,关于更多android jni 开发的知识我还会继续探索。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
在使用Android Studio时,CMakeLists.txt文件是用来配置C/C++项目的构建脚本。它指导Android Studio如何编译和构建项目中的C/C++源代码。 如果你想要在Android Studio中使用CMakeLists.txt,你需要将其放置在你的项目根目录或者是你的模块目录下。然后,你可以在build.gradle文件中引用该CMakeLists.txt文件,以便Android Studio能够正确地构建你的C/C++代码。 如果你在使用CMakeLists.txt编译时遇到了问题,比如出现了错误提示,你可以尝试删除CMakeLists.txt所在文件夹下的.externalNativeBuild文件夹,然后重新打包你的项目。这样做有时可以解决一些构建问题。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [C++版本使用CMakeLists.txt编译(同目录多个源文件)可执行文件](https://download.csdn.net/download/Zhangyanfeng1/12603209)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [AndroidStudio|CMakeLists.txt报错_该用户还没想到昵称_新浪博客](https://blog.csdn.net/weixin_39766005/article/details/109655117)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值