引入so文件CmakeList 配置

做NDK开发的时候,很大程度的是引用成熟类库,我们做逻辑,功能,那么怎么把so文件引入到我们的android工程里面的?
目前AndroidStudio2.2之后官方推荐的是Cmake,之前是Android.mk构建文件,大概类似,我们在只讲解一下Cmake,知识总是要及时更新才是。
通常我们默认编写的时候他会自动书写Cmake

#将指定的源文件生成链接文件,然后添加到工程中去
add_library(
#设置lib的名字
             native-lib
#设置lib的类型 共享型  还有STATIC、和MODULE
             SHARED
 #源码的相对路径
              src/main/cpp/native-lib.cpp
          )
#这个地方是 系统已经集成了很多lib,然后需要find以下 作为声明,表示你要用这些lib
find_library(
#自己起一个 lib的名称
              log-lib

 #lib的文件名
              log )

#将目标文件与库文件进行链接
target_link_libraries( # Specifies the target library.
                       native-lib
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

这是系统根据我们写的C/C++文件生成的,但是我们要加入第三方so怎么配置呢?我就用FFmpeg和Libyuv.so来演示一下。
首先最大的不同是:
需要声明一下文件的基地址,这个后面地址的时候需要用到

set(distribution_DIR ../../../../libs)

然后

#添加lib,SHARED类型,是IMPORTED 引入的库
add_library( libyuv
             SHARED
             IMPORTED)
#设置 库的属性   里面是名称 ,属性:引入地址把我们的真实地址填写进去
set_target_properties( libyuv
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/armeabi-v7a/libyuv.so)

最后

target_link_libraries( 
                       native-lib
#指定链接库
                       libyuv
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

这是ffmpeg和libyuv的配置

# 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)

# 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

             # Provides a relative path to your source file(s).
              src/main/cpp/native-lib.cpp
             src/main/cpp/mylo.h)

# 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.



set(distribution_DIR ../../../../libs)
add_library( libyuv
             SHARED
             IMPORTED)
set_target_properties( libyuv
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/armeabi-v7a/libyuv.so)

add_library( avcodec
             SHARED
             IMPORTED)
set_target_properties( avcodec
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/armeabi-v7a/libavcodec.so)

add_library( avfilter
             SHARED
             IMPORTED)
set_target_properties( avfilter
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/armeabi-v7a/libavfilter.so)

add_library( avformat
             SHARED
             IMPORTED)
set_target_properties( avformat
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/armeabi-v7a/libavformat.so)

add_library( avutil
             SHARED
             IMPORTED)
set_target_properties( avutil
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/armeabi-v7a/libavutil.so)

add_library( swresample
             SHARED
             IMPORTED)
set_target_properties( swresample
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/armeabi-v7a/libswresample.so)

add_library( swscale
             SHARED
             IMPORTED)
set_target_properties( swscale
                       PROPERTIES IMPORTED_LOCATION
                       ${distribution_DIR}/armeabi-v7a/libswscale.so)

include_directories(libs/include)

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 )
              find_library( # Sets the name of the path variable.
                            android-lib

                            # Specifies the name of the NDK library that
                            # you want CMake to locate.
                            android )

# 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.
                       native-lib
                       libyuv
                       avcodec
                       avfilter
                       avformat
                       avutil
                       swresample
                       swscale
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib}
                         ${android-lib})

最后的最后不要忘记在build.gradle(app)中配置

 externalNativeBuild {
            cmake {
                cppFlags "-frtti -fexceptions"
            }
        }
        ndk {
            //选择要添加的对应cpu类型的.so库。
            abiFilters 'armeabi-v7a'
            // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
        }
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\]和引用\[2\]的内容,配置OpenCV的CMakeList文件可以按照以下步骤进行: 1. 首先,确保已经安装了必要的依赖项。可以使用以下命令安装依赖项: ``` sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev ``` 2. 下载OpenCV的源代码和额外模块。可以使用以下命令下载并解压源代码: ``` wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.x.zip unzip opencv.zip unzip opencv_contrib.zip ``` 3. 创建一个build目录,并进入该目录: ``` mkdir -p build && cd build ``` 4. 使用CMake配置项目。可以使用以下命令进行配置: ``` cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules ../opencv-4.x ``` 5. 使用make命令编译项目: ``` make ``` 6. 配置完成后,可以使用find_package命令在CMakeList文件引入OpenCV。可以按照以下方式进行配置: ``` find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) target_link_libraries(your_target_name ${OpenCV_LIBS}) ``` 请注意,这只是一个基本的配置示例,具体的配置可能因项目的不同而有所差异。您可能需要根据您的项目需求进行适当的调整。 #### 引用[.reference_title] - *1* *2* *3* [opencv在 Cmakelist的写法以及编译详解](https://blog.csdn.net/qq_41612863/article/details/122149124)[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^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值