Android-CMakeLists.txt 链接第三方库(动态或者静态库)到自己的生成库中

最近在做关于NDK开发的项目,编译方式通过cmake。
如何将第三方动态链接库连接到自己生成的动态库中,按照以下步骤:

1.首先看目录结构:首先将第三方库复制到jniLibs下,并创建对应的CUP平台目录

在这里插入图片描述

2. 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)

message(STATUS "******************************************************************")
message(STATUS "CMAKE_SOURCE_DIR->" ${CMAKE_SOURCE_DIR})
message(STATUS "PROJECT_SOURCE_DIR->)" ${PROJECT_SOURCE_DIR})
message(STATUS "******************************************************************")

#1.搜索源文件并赋值给变量名
aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app CLIENT_SRC)

aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/user/source USER_SRC)
aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/user USER_OP_SRC)

aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/device/source DEVICE_SRC)
aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/device DEVICE_OP_SRC)

aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/event/source EVENT_SRC)
aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/event EVENT_OP_SRC)

aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/tools TOOLS_SRC)
aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/helper HELPER_SRC)

#2.设置源文件到统一的变量
set(DIR_SRCS ${CLIENT_SRC} ${USER_SRC} ${USER_OP_SRC}
   ${DEVICE_SRC} ${DEVICE_OP_SRC} ${EVENT_SRC} ${EVENT_OP_SRC} ${TOOLS_SRC} ${HELPER_SRC})

#3.设置第三方库头文件所在位置
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/thirdInclude/)

#4.设置第三方库头库所在位置
link_directories(${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/)

#5.对应的库
link_libraries(SKYhttpClient.so SKYmxml.so)

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

#6.创建动态库
add_library( # Sets the name of the library.
             skyqcloud-sdk

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             ${DIR_SRCS}
             ${CMAKE_SOURCE_DIR}/src/main/cpp/skyqc_sdk_native.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.

#7.链接需要的第三方动态库
target_link_libraries( # Specifies the target library.
                       skyqcloud-sdk

                       #the third library
                       SKYhttpClient
                       SKYmxml

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

方式二:

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

message(STATUS "******************************************************************")
message(STATUS "CMAKE_SOURCE_DIR->" ${CMAKE_SOURCE_DIR})
message(STATUS "PROJECT_SOURCE_DIR->)" ${PROJECT_SOURCE_DIR})
message(STATUS "******************************************************************")

#1.搜索源文件并赋值给变量名
aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app CLIENT_SRC)

aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/user/source USER_SRC)
aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/user USER_OP_SRC)

aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/device/source DEVICE_SRC)
aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/device DEVICE_OP_SRC)

aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/event/source EVENT_SRC)
aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/event EVENT_OP_SRC)

aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/tools TOOLS_SRC)
aux_source_directory(${CMAKE_SOURCE_DIR}/src/main/cpp/common/app/helper HELPER_SRC)

#2.设置源文件到统一的变量
set(DIR_SRCS ${CLIENT_SRC} ${USER_SRC} ${USER_OP_SRC}
   ${DEVICE_SRC} ${DEVICE_OP_SRC} ${EVENT_SRC} ${EVENT_OP_SRC} ${TOOLS_SRC} ${HELPER_SRC})

#3.设置第三方库头文件所在位置
include_directories(${CMAKE_SOURCE_DIR}/src/main/cpp/thirdInclude/)

#4.导入第三方动态库
# 添加第三方动态库
add_library(SKYhttpClient
             SHARED
             IMPORTED)

# 设置第三方动态库属性(存储位置) armeabi-v7a
set_target_properties(SKYhttpClient
                      PROPERTIES IMPORTED_LOCATION
                      ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libSKYhttpClient.so)

# 添加第三方动态库
add_library(SKYmxml
             SHARED
             IMPORTED)

# 设置第三方动态库属性(存储位置)
set_target_properties(SKYmxml
                      PROPERTIES IMPORTED_LOCATION
                      ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libSKYmxml.so)


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

#6.创建动态库(自己将要生成的动态库)
add_library( # Sets the name of the library.
             skyqcloud-sdk

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             ${DIR_SRCS}
             ${CMAKE_SOURCE_DIR}/src/main/cpp/skyqc_sdk_native.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.

#6.链接需要的第三方动态库
target_link_libraries( # Specifies the target library.
                       skyqcloud-sdk

                       #the third library
                       SKYhttpClient
                       SKYmxml

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

注意:

若第三方库初始化等失败,则需要在Java文件中设置,类似:

static {
      System.loadLibrary("device");
      System.loadLibrary("ControllerAdapter");
  }

或者在初始化库函数之前,在cpp中:

#include <dlfcn.h>  
      
void *handle;  
  
handle = dlopen ("libm.so", RTLD_NOW, RTLD_LOCAL);  
if (!handle) 
{  
	fprintf (stderr, "%s ", dlerror());  
    exit(1);  
}  
      
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
CMakeLists.txt可以通过定义变量INC_DIR和LINK_DIR来加载第三方库。INC_DIR用来指定头文件路径,而LINK_DIR用来指定的路径。在CMakeLists.txt,可以使用include_directories命令将INC_DIR添加到头文件搜索路径,使用link_directories命令将LINK_DIR添加到的搜索路径。这样就可以在项目使用第三方库了。如果需要生成自己的,需要在CMakeLists.txt导入第三方库的具体步骤如下:首先,使用find_package命令找到需要的,例如find_package(wfdb REQUIRED)。然后,使用include_directories命令将找到的的头文件路径添加到头文件搜索路径下来,使用target_link_libraries命令将找到的到目标或可执行文件,例如target_link_libraries(mylibrary wfdb)。最后,使用add_library命令生成自己的,例如add_library(mylibrary SHARED mylibrary.cpp)。这样就完成了CMakeLists.txt加载第三方库的过程。<span class="em">1</span><span class="em">2</span><span class="em">3</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_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [使用CMake导入第三方库](https://blog.csdn.net/alan711/article/details/86248612)[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_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值