Undefined reference error, can not create shared library

▼魔方 西西 提交于 2020-02-03 08:47:47

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/27519462/undefined-reference-error-can-not-create-shared-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!