zoukankan      html  css  js  c++  java
  • AndroidStudio2.2.x以上使用cMake编译调用底层c生成依赖库

    最近使用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 as 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 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.
    
                   aux_source_directory(src/main/cpp SRC_LIST)
                   add_library( # Sets the name of the library.
                                native-lib
    
                                # Sets the library as 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" 即可。这样重新编译运行,一切正常!

  • 相关阅读:
    快照原理及场景
    CEP实时分析模型
    请求响应模式
    JMS消息服务模型
    EMF与GEF
    基于SOA的编程模型
    实时计算CEP
    数据库常见的场景
    自签证书服务加入证书验证
    post提交主订单数据(gateway)实现httpapi
  • 原文地址:https://www.cnblogs.com/BobGo/p/7341581.html
Copyright © 2011-2022 走看看