Android CMakeList引入第三方动态库
2020-06-17 本文已影响0人
编程的猫
在CMakeList中需要编译自己的动态库,同时引入第三方动态库的CMakeList的写法如下:
cmake_minimum_required(VERSION 3.4.1)
# ${CMAKE_SOURCE_DIR} 表示CMakeLists当前的文件目录
set(include ${CMAKE_SOURCE_DIR}/include)
set(jniLibsDir ${CMAKE_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
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).
native-lib.cpp JavaListener.cpp PlayPCM.cpp
AudioEncoder.c
VodDecoder.c
)
#avcodec
add_library(avcodec SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION
${jniLibsDir}/libavcodec.so)
#avfilter
add_library(avfilter SHARED IMPORTED)
set_target_properties(avfilter PROPERTIES IMPORTED_LOCATION
${jniLibsDir}/libavfilter.so)
#avformat
add_library(avformat SHARED IMPORTED)
set_target_properties(avformat PROPERTIES IMPORTED_LOCATION
${jniLibsDir}/libavformat.so)
#avutil
add_library(avutil SHARED IMPORTED)
set_target_properties(avutil PROPERTIES IMPORTED_LOCATION
${jniLibsDir}/libavutil.so)
#avswresample
add_library(swresample SHARED IMPORTED)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION
${jniLibsDir}/libswresample.so)
#avswscale
add_library(swscale SHARED IMPORTED)
set_target_properties(swscale PROPERTIES IMPORTED_LOCATION
${jniLibsDir}/libswscale.so)
#引入头文件
include_directories(${CMAKE_SOURCE_DIR}/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)
#链接需要的库
target_link_libraries( # Specifies the target library.
native-lib
${avcodec-lib}
${avfilter-lib}
${avformat-lib}
${avutil-lib}
${swresample-lib}
${swscale-lib}
OpenSLES
${android-lib}
# Links the target library to the log library
# included in the NDK.
${log-lib})
文件中的注释已经写的很清楚了,这里不再做多余的解释
我的C++/C文件和头文件的目录结构(include里边是头文件):
image.png
以下示例是在C文件中引用:
#include <stdio.h>
#include <time.h>
#include "include/VodDecoder.h"
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#include "libavutil/log.h"