欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-05-14 08:06:09
...

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

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

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

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);  
}  
      
相关标签: 基础 android