Undefined link error, cannot create shared library
Tried so many ways to solve the problem but no luck. Here is my Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := avcodec
LOCAL_SRC_FILES :=libavcodec.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avutil
LOCAL_SRC_FILES :=libavutil.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := swscale
LOCAL_SRC_FILES :=libswscale.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avformat
LOCAL_SRC_FILES :=libavformat.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := avresample
LOCAL_SRC_FILES :=libavresample.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := swresample
LOCAL_SRC_FILES :=libswresample.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := x264
LOCAL_SRC_FILES :=libx264.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
#------------------------------------------
LOCAL_C_INCLUDES := $(LOCAL_PATH)\
$(LOCAL_PATH)/include
LOCAL_MODULE := media
LOCAL_SRC_FILES := \
P_Decoding.cpp \
P_Encoding.cpp \
wrapper_for_ffmpeg.cpp
LOCAL_CPPFLAGS := -std=c++11 -pthread -frtti -fexceptions
LOCAL_ARM_MODE := arm
LOCAL_STATIC_LIBRARIES := avcodec avutil swscale avformat avresample swresample x264
LOCAL_LDLIBS := -llog -ljnigraphics -landroid -lz -lEGL
LOCAL_CPP_FEATURES := rtti exceptions
include $(BUILD_SHARED_LIBRARY)
The jni folder contains all the .hpp header files associated with the .cpp files in the makefile, there are also all the static .a libraries and an "include" folder where all the header files are linked to the static libraries. I am using the ndk9c version. Here is part of the error log:
[armeabi-v7a] Compile++ thumb: media <= P_Decoding.cpp
[armeabi-v7a] Compile++ thumb: media <= P_Encoding.cpp
[armeabi-v7a] Compile++ thumb: media <= wrapper_for_ffmpeg.cpp
/home/pro/android-ndk-r9c/toolchains/llvm-3.3/prebuilt/linux-x86/bin/clang++ -Wl, -soname, libmedia.so -shared --sysroot = / home / pro / android -ndk- r9c / platform / android-9 / arch-arm. / obj / local / armeabi / objs / media / P_Decoding.o. / obj / local / armeabi / objs / media / P_Encoding.o jni / libavformat.a jni / libavcodec. a jni / libavutil.a jni / libswscale.a -lgcc./obj/local/armeabi/libgnustl_shared.so -gcc-toolchain / home / pro / android-ndk-r9c / toolchains / arm-linux-androideabi- 4.8 / prebuilt / linux-x86 -no-canonical-prefixes -target armv5te-none-linux-androideabi -Wl, - no- undefined -Wl, -z, noexecstack -Wl, -z, relro -Wl, - z, now -L / home / pro / android-ndk-r9c / platform / android-9 / arch-arm / usr / lib -llog -ljnigraphics -lz -ldl -lgcc / home / pro / android-ndk-r9c / Sources / cxx-stl / gnu-libstdc ++ / 4.8 / libs / armeabi / libsupc ++. a -lc -lm -o./obj/local/armeabi/libmedia.so
jni/P_Decoding.cpp:10: error: undefined reference to 'avcodec_register_all'
jni/P_Decoding.cpp:17: error: undefined reference to 'avcodec_find_decoder'
jni/P_Decoding.cpp:14: error: undefined reference to 'avcodec_find_decoder_by_name'
jni/P_Decoding.cpp:24: error: undefined reference to 'avcodec_alloc_context3'
jni/P_Decoding.cpp:50: error: undefined reference to 'avcodec_open2'
jni/P_Decoding.cpp:56: error: undefined reference to 'avcodec_alloc_frame'
jni/P_Decoding.cpp:76: error: undefined reference to 'sws_getContext'
jni/P_Decoding.cpp:90: error: undefined reference to 'avcodec_decode_video2'
jni/P_Decoding.cpp:104: error: undefined reference to 'sws_scale'
jni/P_Decoding.cpp:118: error: undefined reference to 'avcodec_close'
Any help please. thanks in advance
source to share
Make sure you add a block extern "C"
around the libavcodec inclusion and swscale headers for example. eg:
extern "C" {
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
See https://libav.org/faq.html#I_0027m-using-Libav-from-within-my-C_002b_002b-application-but-the-linker-complains-about-missing-symbols-which-seem-to- be-available_002e for documentation that explains this.
EDIT: Are you sure you prebuilt libavcodec.a for the same architecture (armeabi-v7a) when you build? If you are building your shared library for multiple architectures (armeabi-v7a, x86, etc.), you will also need to have multiple corresponding versions of the prebuilt libraries.
source to share