CMake使⽤简介及编写
在新建⼯程的第⼀步选择Native C++,⼯程建好后会在app\src\main\cpp⽬录下⽣成。
设置CMake需要的最⼩版本
#设置CMake需要的最⼩版本
cmake_minimum_required(VERSION 3.4.1)
添加源⽂件让CMake编译成共享库
add_library(
ffmpeg-cmd
SHARED
ffmpeg/ffmpeg-cmd.cpp ffmpeg/ffmpeg.c ffmpeg/cmdutils.c ffmpeg/ffmpeg_filter.c ffmpeg/ffmpeg_hw.c ffmpeg/ffmpeg_opt.c
)
#指定头⽂件所在路径,相对于所在路径
include_directories(ffmpeg/)
ffmpeg-cmd-指定共享库名称,库⽂件名称会⾃动加上前缀lib变成libffmpeg-cmd.so,但是加载的时候仍然使⽤指定的名称:System.loadLibrary("ffmpeg-cmd");
SHARED-指定⽣成共享库
ffmpeg/ffmpeg-cmd.cpp 指定源码路径,多个源⽂件⽤空格隔开,注意是相对的路径
添加预编译的库
add_library(avcodec #指定引⼊模块的名称
SHARED
IMPORTED
)
SET_TARGET_PROPERTIES(
avcodec #指定模块名称
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/ffmpeg/prebuilt/${ANDROID_ABI}/libavcodec.so
)
#指定模块头⽂件相对路径,如果已经指定过相同路径不需要重复指定
include_directories(ffmpeg/)
${PROJECT_SOURCE_DIR}表⽰所在路径
如果有多个版本ABI的库⽂件,在编译的时候${ANDROID_ABI}会被替换成相应的abi名称。⽐如在gradle中做了如下配置
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
CMake在编译的时候会分别到以下路径去:
…src/main/cpp/ffmpeg/prebuilt/armeabi-v7a/libavcodec.so
…src/main/cpp/ffmpeg/prebuilt/arm64-v8a/libavcodec.so
添加NDK API
添加⽇志⽀持
find_library(
log-lib
log)
链接
为了在我们⾃⼰的库中能够调⽤其它库函数,需要设置target_link_libraries()
target_link_libraries(
ffmpeg-cmd #我们⾃⼰的库
avcodec
swscale
swresample
postproc
avutil
avformat
avfilter
${log-lib})
举个栗⼦
下⾯的⽰例演⽰了如何引⼊预编译的FFmpeg供⾃⼰调⽤:
cmake_minimum_required(VERSION 3.4.1)
#add libavcodec
add_library(avcodec
SHARED
IMPORTED
)
SET_TARGET_PROPERTIES(
avcodec
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/ffmpeg/prebuilt/${ANDROID_ABI}/libavcodec.so
)
#add libavfilter
add_library(avfilter
SHARED
IMPORTED
)
SET_TARGET_PROPERTIES(
avfilter
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/ffmpeg/prebuilt/${ANDROID_ABI}/libavfilter.so
)
#add libavformat
add_library(avformat
SHARED
IMPORTED
)
SET_TARGET_PROPERTIES(
avformat
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/ffmpeg/prebuilt/${ANDROID_ABI}/libavformat.so
)
#add libavutil
add_library(avutil
SHARED
IMPORTED
)
SET_TARGET_PROPERTIES(
avutil
avutil
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/ffmpeg/prebuilt/${ANDROID_ABI}/libavutil.so
)
#add libpostproc
add_library(postproc
SHARED
IMPORTED
)
SET_TARGET_PROPERTIES(
postproc
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/ffmpeg/prebuilt/${ANDROID_ABI}/libpostproc.so
)
#add libswresample
add_library(swresample
SHARED
IMPORTED
)
SET_TARGET_PROPERTIES(
swresample
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/ffmpeg/prebuilt/${ANDROID_ABI}/libswresample.so
)
#add libswscale
add_library(swscale
SHARED
IMPORTED
)
SET_TARGET_PROPERTIES(
swscale
PROPERTIES IMPORTED_LOCATION
${PROJECT_SOURCE_DIR}/ffmpeg/prebuilt/${ANDROID_ABI}/libswscale.so
)
include_directories(ffmpeg/)
find_library(
log-lib
log)
add_library(
ffmpeg-cmd
SHARED
ffmpeg/ffmpeg-cmd.cpp ffmpeg/ffmpeg.c ffmpeg/cmdutils.c ffmpeg/ffmpeg_filter.c ffmpeg/ffmpeg_hw.c ffmpeg/ffmpeg_opt.c )
target_link_libraries( # Specifies the target library.
ffmpeg-cmd
avcodec
swscale
swresample
postproc
avutil
avformat
avfilter
${log-lib})
⽬录结构:
可能的报错
1.SET_TARGET_PROPERTIES()中库⽂件路径指定错误:
ninja: error: 'ffmpeg/prebuilt/arm64-v8a/libswscale.so', needed by 'D:/Android/Demo/AVDemo/app/build/intermediates/cmake/debug/obj/arm64-v8a/libffmp eg-cmd.so', missing and no known rule to make it
2.没有指定需要的头⽂件或路径错误:
fatal error: 'libavcodec/mathops.h' file not foundcmake如何使用

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。