编译环境如下:
- 电脑 Mac Os 13.1
- NDK 版本: android-ndk-r17c
- ffmpeg 版本: ffmpeg-4.1.2
( 开发环境都是最新的, 编译环境可以不需要最新 )
直接使用 github 上的一个 shell
脚本, 就可以编译出多 CPU 架构的动/静态库
可以在配置中, 可以去掉一些不需要编译进来的文件.
在编译过程中正常, 但是在测试使用的时候, 发现一直报错.
extern "C"{
#include <libavutil/avutil.h>
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_aaatest_studyffmpeg_MainActivity_stringFromJNI(JNIEnv* env, jobject thisz) {
std::string hello = "Hello from C++ ffmpeg version = ";
hello.append(av_version_info());
return env->NewStringUTF(hello.c_str());
}
很简单的一段代码, 报错 undefined reference to 'stderr'
. 晚上找了解决方案, 需要在脚本的 --extra-cflags
中加上 -D__ANDROID_API__=21
这个21和脚本中 PLATFORM=android-21
对应.
脚本如下, 有点长, 配置项里面,只打了 arm64-v8a
架构的静态库. 剩下的都没有改动.
#!/usr/bin/env bash
NDK_HOME=/Users/yaqi/Library/Android/sdk/ndk/17.2.4988734
PREFIX=android-build
HOST_PLATFORM=darwin-x86_64
PLATFORM=android-21
PLATFORM_64=android-21
CONFIG_LOG_PATH=${PREFIX}/log
COMMON_OPTIONS=
CONFIGURATION=
build(){
APP_ABI=$1
echo "======== > Start build $APP_ABI"
case ${APP_ABI} in
# armeabi )
# ARCH="arm"
# CPU="armv6"
# MARCH="armv6"
# TOOLCHAINS="$NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/$HOST_PLATFORM"
# CROSS_PREFIX="$TOOLCHAINS/bin/arm-linux-androideabi-"
# SYSROOT="$NDK_HOME/platforms/$PLATFORM/arch-arm"
# EXTRA_CFLAGS="-march=$MARCH"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -mfloat-abi=softfp -mfpu=vfp"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -I$NDK_HOME/sysroot/usr/include/arm-linux-androideabi"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -isysroot $NDK_HOME/sysroot"
# EXTRA_LDFLAGS="-lc -lm -ldl -llog"
# EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-rpath-link=$SYSROOT/usr/lib -L$SYSROOT/usr/lib"
# EXTRA_OPTIONS="--disable-x86asm"
# ;;
# armeabi-v7a )
# ARCH="arm"
# CPU="armv7-a"
# MARCH="armv7-a"
# TOOLCHAINS="$NDK_HOME/toolchains/arm-linux-androideabi-4.9/prebuilt/$HOST_PLATFORM"
# CROSS_PREFIX="$TOOLCHAINS/bin/arm-linux-androideabi-"
# SYSROOT="$NDK_HOME/platforms/$PLATFORM/arch-arm"
# EXTRA_CFLAGS="-march=$MARCH"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -mfloat-abi=softfp -mfpu=vfpv3-d16"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -I$NDK_HOME/sysroot/usr/include/arm-linux-androideabi"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -isysroot $NDK_HOME/sysroot"
# EXTRA_LDFLAGS="-lc -lm -ldl -llog"
# EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,--fix-cortex-a8"
# EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-rpath-link=$SYSROOT/usr/lib -L$SYSROOT/usr/lib"
# EXTRA_OPTIONS="--enable-neon --disable-x86asm"
# ;;
arm64-v8a )
ARCH="aarch64"
CPU="armv8-a"
MARCH="armv8-a"
TOOLCHAINS="$NDK_HOME/toolchains/aarch64-linux-android-4.9/prebuilt/$HOST_PLATFORM"
CROSS_PREFIX="$TOOLCHAINS/bin/aarch64-linux-android-"
SYSROOT="$NDK_HOME/platforms/$PLATFORM_64/arch-arm64"
EXTRA_CFLAGS="-march=$MARCH"
EXTRA_CFLAGS="$EXTRA_CFLAGS -I$NDK_HOME/sysroot/usr/include/aarch64-linux-android"
EXTRA_CFLAGS="$EXTRA_CFLAGS -isysroot $NDK_HOME/sysroot"
EXTRA_LDFLAGS="-lc -lm -ldl -llog"
EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-rpath-link=$SYSROOT/usr/lib -L$SYSROOT/usr/lib"
EXTRA_OPTIONS="--enable-neon --disable-x86asm"
;;
# x86 )
# ARCH="x86"
# CPU="i686"
# MARCH="i686"
# TOOLCHAINS="$NDK_HOME/toolchains/x86-4.9/prebuilt/$HOST_PLATFORM"
# CROSS_PREFIX="$TOOLCHAINS/bin/i686-linux-android-"
# SYSROOT="$NDK_HOME/platforms/$PLATFORM/arch-x86"
# EXTRA_CFLAGS="-march=$MARCH"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -mtune=intel -mssse3 -mfpmath=sse -m32"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -I$NDK_HOME/sysroot/usr/include/i686-linux-android"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -isysroot $NDK_HOME/sysroot"
# EXTRA_LDFLAGS="-lc -lm -ldl -llog"
# EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-rpath-link=$SYSROOT/usr/lib -L$SYSROOT/usr/lib"
# EXTRA_OPTIONS="--disable-asm"
# ;;
# x86_64 )
# ARCH="x86_64"
# CPU="x86_64"
# MARCH="x86-64"
# TOOLCHAINS="$NDK_HOME/toolchains/x86_64-4.9/prebuilt/$HOST_PLATFORM"
# CROSS_PREFIX="$TOOLCHAINS/bin/x86_64-linux-android-"
# SYSROOT="$NDK_HOME/platforms/$PLATFORM_64/arch-x86_64"
# EXTRA_CFLAGS="-march=$MARCH"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -mtune=intel -msse4.2 -mpopcnt -m64"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -I$NDK_HOME/sysroot/usr/include/x86_64-linux-android"
# EXTRA_CFLAGS="$EXTRA_CFLAGS -isysroot $NDK_HOME/sysroot"
# EXTRA_LDFLAGS="-lc -lm -ldl -llog"
# EXTRA_LDFLAGS="$EXTRA_LDFLAGS -Wl,-rpath-link=$SYSROOT/usr/lib64 -L$SYSROOT/usr/lib64"
# EXTRA_OPTIONS="--disable-asm"
# ;;
esac
echo "-------- > Start clean workspace"
make clean
echo "-------- > Start build configuration"
CONFIGURATION="$COMMON_OPTIONS"
CONFIGURATION="$CONFIGURATION --logfile=$CONFIG_LOG_PATH/config_$APP_ABI.log"
CONFIGURATION="$CONFIGURATION --prefix=$PREFIX"
CONFIGURATION="$CONFIGURATION --libdir=$PREFIX/libs/$APP_ABI"
CONFIGURATION="$CONFIGURATION --incdir=$PREFIX/includes/$APP_ABI"
CONFIGURATION="$CONFIGURATION --pkgconfigdir=$PREFIX/pkgconfig/$APP_ABI"
CONFIGURATION="$CONFIGURATION --arch=$ARCH"
CONFIGURATION="$CONFIGURATION --cpu=$CPU"
CONFIGURATION="$CONFIGURATION --cross-prefix=$CROSS_PREFIX"
CONFIGURATION="$CONFIGURATION --sysroot=$SYSROOT"
CONFIGURATION="$CONFIGURATION --extra-ldexeflags=-pie"
CONFIGURATION="$CONFIGURATION $EXTRA_OPTIONS"
echo "-------- > Start config makefile with $CONFIGURATION --extra-cflags="${EXTRA_CFLAGS}" --extra-ldflags="${EXTRA_LDFLAGS}""
./configure ${CONFIGURATION} \
--extra-cflags="$EXTRA_CFLAGS -D__ANDROID_API__=21" \
--extra-ldflags="$EXTRA_LDFLAGS"
echo "-------- > Start make $APP_ABI with -j8"
make j8
echo "-------- > Start install $APP_ABI"
make install
echo "++++++++ > make and install $APP_ABI complete."
}
build_all(){
COMMON_OPTIONS="$COMMON_OPTIONS --target-os=android"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-static"
COMMON_OPTIONS="$COMMON_OPTIONS --disable-shared"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-protocols"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-cross-compile"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-optimizations"
COMMON_OPTIONS="$COMMON_OPTIONS --disable-debug"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-small"
COMMON_OPTIONS="$COMMON_OPTIONS --disable-doc"
COMMON_OPTIONS="$COMMON_OPTIONS --disable-programs"
COMMON_OPTIONS="$COMMON_OPTIONS --disable-ffmpeg"
COMMON_OPTIONS="$COMMON_OPTIONS --disable-ffplay"
COMMON_OPTIONS="$COMMON_OPTIONS --disable-ffprobe"
COMMON_OPTIONS="$COMMON_OPTIONS --disable-symver"
COMMON_OPTIONS="$COMMON_OPTIONS --disable-network"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-pthreads"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-mediacodec"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-jni"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-zlib"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-pic"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-avresample"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-decoder=h264"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-decoder=mpeg4"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-decoder=mjpeg"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-decoder=png"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-decoder=vorbis"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-decoder=opus"
COMMON_OPTIONS="$COMMON_OPTIONS --enable-decoder=flac"
echo "COMMON_OPTIONS=$COMMON_OPTIONS"
echo "PREFIX=$PREFIX"
echo "CONFIG_LOG_PATH=$CONFIG_LOG_PATH"
mkdir -p ${CONFIG_LOG_PATH}
# build $app_abi
#build "armeabi"
#build "armeabi-v7a"
build "arm64-v8a"
#build "x86"
#build "x86_64"
}
echo "-------- Start --------"
build_all
echo "-------- End --------"
工程目录结构如下 :
cpp -> arm64-v8a 存放的是静态库文件
cpp -> includes 下存放的是头文件
CMakeList.txt 内容如下
cmake_minimum_required(VERSION 3.22.1)
project("studyffmpeg")
#导入头文件
include_directories(${CMAKE_SOURCE_DIR}/includes)
#导入静态库
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/arm64-v8a")
add_library( # Sets the name of the library.
studyffmpeg
SHARED
native-lib.cpp)
#这里的几个库文件顺序最好别搞错.
target_link_libraries(
studyffmpeg
avformat
avcodec
avdevice
avfilter
avutil
swresample
swscale
log)
网友评论