clang++: error: linker command failed with exit code 1 (use -v to see invocation) in cpp with ffmpeg

clang++: error: linker command failed with exit code 1 (use -v to see invocation) in cpp with ffmpeg

clang++: error: linker command failed with exit code 1 (use -v to see invocation)

我看到了这个link and link2。但是,它没有完成。

我的 jni 文件夹:

Android.mk 文件

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += ./include
LOCAL_MODULE     := native-lib
LOCAL_CFLAGS := -DSTDC_HEADERS -std=c99
LOCAL_CFLAGS := -Wno-pointer-sign
LOCAL_ARM_MODE := arm
APP_OPTIM := release
LOCAL_SRC_FILES  := \
./native-lib.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

原生-lib.cpp文件

#include <jni.h>
#include <string>

extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/opt.h"
}

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_m_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
av_register_all();
return env->NewStringUTF(hello.c_str());
}

当我构建 ndk 时出现此错误。

D:\Github\n>ndk-build
Android NDK: APP_PLATFORM not set. Defaulting to minimum supported version android-16.
[arm64-v8a] Compile++      : native-lib <= native-lib.cpp
[arm64-v8a] SharedLibrary  : libnative-lib.so
./obj/local/arm64-v8a/objs/native-lib/./libmp3lame/native-lib.o: In function `Java_com_example_m_MainActivity_stringFromJNI':
D:\Github\n/jni/./libmp3lame/native-lib.cpp:16: undefined reference to `av_register_all'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [D:/install/sdk/ndk/21.0.6113669/build//../build/core/build-binary.mk:725: obj/local/arm64-v8a/libnative-lib.so] Error 1

D:\Github\n>ndk-build -v
GNU Make 4.2.1
Built for x86_64-w64-mingw32
Copyright (C) 1988-2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

D:\Github\n>

为什么会这样native-lib.cpp:16: undefined reference av_register_all?

我在我的 jni 文件夹中添加了所有必需的库,但是,为什么会出现此错误和此 native-lib.cpp:16: undefined reference av_register_all? 我该如何解决这个问题?

I added all the required libs in my jni folder but, why this error and this native-lib.cpp:16: undefined reference av_register_all comes?

仅将库放入项目中是不够的。您的 Android.mk 中没有任何 link 指令。所以即使你的库在那里,当你的 native-lib 被编译时它也不会被 linked。因此你未定义的引用...

我建议您查看 this post,重点关注那些 LOCAL_C_INCLUDESLOCAL_LDLIBS 变量。您必须在您的项目中执行类似的操作。