AndroidStudio2.2.x以上使用cMake编译调用底层c生成依赖库

AndroidStudio2.2.x以上使用cMake编译调用底层c生成依赖库,第1张

概述最近使用AndroidStudio的最新ndk编译方式cMake来编译底层cpp文件,由于之前没有接触过cMake语法,先附上官方学习文档地址:https://developer.android.co

最近使用AndroIDStudio的最新ndk编译方式cMake来编译底层cpp文件,由于之前没有接触过cMake语法,先附上官方学习文档地址:https://developer.androID.com/ndk/guIDes/cmake.HTML,以及友情中文翻译网址:https://www.zybuluo.com/khan-lau/note/254724

底层c文件一大堆,如下图所示

 

问题一:

其中native-lib.cpp是提供对外接口的,所以对其他文件的依赖都写在了该文件中,接下来直接编译吗?no,那样你会得到编译错误,在native-lib.cpp中找不到其他c文件里边的函数。所以是cMake编译的时候没有把那些c文件编译进去,这样要去CMakeLists.txt中去添加文件了。

未修改之前的CMakeLists.txt文件内容是这样的:

# 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  a shared library.                            SHARED                                              EXCLUDE_FROM_ALL                            # ProvIDes a relative path to your source file(s).                            src/main/cpp/native-lib.cpp                             )

学习CMake的官方文档可以知道,其中的add_library()正是设置生成包的地方,这里只有native-lib.cpp一个文件,理论上应该要包括其他那些的,那么加上其他那几个文件就好了吧?是可以这样,但是那么多文件,未免也太长了吧,当然CMake肯定预料到这种问题了,所以我们可以get一个新技能,使用aux_source_directory()在源文件位置添加文件列表,而不是单个文件,get修改之后的文件内容如下:

# Sets the minimum version of CMake required to build the native library.               cmake_minimum_required(VERSION )               # Creates and names a library,1)"> you.               # Gradle automatically packages shared librarIEs with your APK.               aux_source_directory(src/main/cpp SRC_List)               add_library( # Sets the name of the library.                            native-lib                            # Sets the library  a shared library.                            SHARED                               EXCLUDE_FROM_ALL                            # ProvIDes a relative path to your source file(s).                            #src/main/cpp/native-lib.cpp                             ${SRC_List}                             )

这样子改完需要Synchronize同步一下,这样就可以重新编译了。

 

问题二:

在编译通过之后,运行调用底层代码,结果程序崩溃了,提示java.lang.UnsatisfIEdlinkError: Native method not found: ***;好吧,没有找到这个底层方法?可是明明已经生成了,仔细观察上图代码结构可以发现,由于AndroIDStudio自动生成的底层文件是.cpp的,也就是使用了c++的语法规则,而其他的底层文件都是.c的使用的c语言规则,他们两者之间还是有区别的,现在需要在cpp文件中调用c文件,那么要在cpp文件中做些修改,首先头文件包含c的是相同的,不同点是在使用c文件中的函数上方,加一行 extern "C" 即可。这样重新编译运行,一切正常!

 

总结

以上是内存溢出为你收集整理的AndroidStudio2.2.x以上使用cMake编译调用底层c生成依赖库全部内容,希望文章能够帮你解决AndroidStudio2.2.x以上使用cMake编译调用底层c生成依赖库所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1119332.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存