4

Tried so many ways to solve 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)

In jni folder there are all header .hpp files related to .cpp files on makefile, also there are all static .a libraries and "include" folder where all header files related to static libraries are. I'm using ndk9c version. Here is part of 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/platforms/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/platforms/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

support_ms
  • 1,873
  • 1
  • 18
  • 33

2 Answers2

3

Make sure you have added the extern "C" block around the include of the libavcodec and swscale headers, e.g. like this:

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: Also, are you sure that the prebuilt libavcodec.a is for the same architecture (armeabi-v7a) as you're building? If you are building your shared library for multiple architectures (armeabi-v7a, x86, etc) you need to have multiple corresponding versions of the prebuilt libraries as well.

mstorsjo
  • 12,983
  • 2
  • 39
  • 62
  • Are you sure you have that code in _every_ place in the whole project where you include any of the libavcodec/libavutil/libswscale headers? – mstorsjo Dec 17 '14 at 07:26
  • Yes. I use only in one space I have – support_ms Dec 17 '14 at 07:32
  • 2
    Are you sure that the prebuilt libavcodec.a is for the same architecture (armeabi-v7a) as you're building? If you are building your shared library for multiple architectures (armeabi-v7a, x86, etc) you need to have multiple corresponding versions of the prebuilt libraries as well. – mstorsjo Dec 17 '14 at 07:40
  • oh, I'm not sure that static libraries has same architecture, Is that thing can be the cause of the problem? Can I prebuilt to corresponding architecture by showing static library and header files? – support_ms Dec 17 '14 at 08:43
  • If it were missing `extern "C"`, then the unresolved symbols would look mangled – Alex Cohn Dec 17 '14 at 17:01
  • The linker doesn't usually show the mangled form, but it would probably contain the signature, like `undefined reference to 'avcodec_find_decoder(AVCodecID)'`, so that's indeed probably not the issue. – mstorsjo Dec 17 '14 at 19:27
  • I compiled static libraries for armeabi-v7a architecture and problem gone @mstorsjo thanks for suggestion. You can change your answer and I will accept it – support_ms Dec 18 '14 at 14:15
  • Updated the answer with this suggestion as well. – mstorsjo Dec 18 '14 at 14:30
0

Try ndk-build V=1 to see the actual command that links your library. You want to end up with

-lavformat -lavcodec -lavfilter -lavutil -lswscale

in that order (as here), but I am not sure where avresample swresample x264 should fit. Rearrange your LOCAL_STATIC_LIBRARIES accordingly.

Community
  • 1
  • 1
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • I added extra log files which are got by ndk-build V=1 – support_ms Dec 18 '14 at 06:11
  • How were the libraries `jni/libavformat.a` and others created? Maybe, as @[mstorsjo](http://stackoverflow.com/users/3115956/mstorsjo) suggested, they were not built for **armeabi**? – Alex Cohn Dec 18 '14 at 06:54