ndk-build:静态链接到libxml.a时对..错误的未定义引用

kra*_*sen 11 android android-ndk

当我使用ndk-build工具编译单个c ++源文件时,我得到了很多未定义的引用...错误.为了记录,我在linux主机系统上使用NDK r6.

首先,我一整天都在反对这个问题,所以如果我要留下一些东西或者解释自己有点快,我很抱歉.请向我询问任何其他信息.

我的源代码有很多我想要静态链接到的依赖库.我在Android.mk文件中定义了所有不同的依赖库,如下所示:

include $(CLEAR_VARS)

LOCAL_MODULE    := <name>
LOCAL_SRC_FILES := lib<name>.a
LOCAL_EXPORT_C_INCLUDES := <header files related to this lib>

include $(PREBUILT_STATIC_LIBRARY)
Run Code Online (Sandbox Code Playgroud)

虽然我确保使用我的Android.mk和Application.mk文件在我的jni /文件夹中以lib.a的形式提供这些文件.(和我的源文件)另外我通过添加APP_STL := stlport_static到我的Application.mk文件添加了STL支持.我的单个源文件(riidr-jni.cpp)目前是:

#include <string>
#include <jni.h>
#include <android/log.h>
#include "../com_riidr_apps_reader_RiidrEbookReaderActivity.h"
#include <dp_all.h>

void Java_com_riidr_apps_reader_RiidrEbookReaderActivity_init ( JNIEnv* env, jobject thiz ) {
    __android_log_write(ANDROID_LOG_INFO, "Riidr", "JNI init - start");
    dp::platformInit( dp::PI_DEFAULT );
}
Run Code Online (Sandbox Code Playgroud)

如果我取消注释该dp::platformInit( dp::PI_DEFAULT );行并将其替换为std::string test = "asd";我没有任何错误,这使我相信因此STL lib被正确导入和链接.

我的Android.mk文件的最后一条指令是这样的

include $(CLEAR_VARS)

LOCAL_MODULE    := riidr-jni
LOCAL_SRC_FILES := riidr-jni.cpp
LOCAL_LDLIBS := -llog -ldl -lz
LOCAL_STATIC_LIBRARIES :=  \
hobbes \
dp \
adept \
mschema \
hobbes \
t3 \
xml \
mschema \
dp \
adept \
xml \
cryptopenssl \
fonts \
png \
jpeg \
ssl \
crypto \
expat \
cts \
hyphen \
curl

include $(BUILD_SHARED_LIBRARY)
Run Code Online (Sandbox Code Playgroud)

运行ndk-build工具时出现的错误如下:

/home/creen/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o):在函数std::_Rb_tree_iterator<std::pair<uft::Value const, xpath::DynamicContext*> >::operator++()': xpath_context.cpp:(.text._ZNSt17_Rb_tree_iteratorISt4pairIKN3uft5ValueEPN5xpath14DynamicContextEEEppEv[std::_Rb_tree_iterator<std::pair<uft::Value const, xpath::DynamicContext*> >::operator++()]+0x1c): undefined reference tostd :: _ Rb_tree_increment(std :: _ Rb_tree_node_base*)'/ home/creen/Projects/Riidr /workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o):在函数std::_Rb_tree<uft::Value, std::pair<uft::Value const, xpath::DynamicContext*>, std::_Select1st<std::pair<uft::Value const, xpath::DynamicContext*> >, xpath::Context::ValueCompare, std::allocator<std::pair<uft::Value const, xpath::DynamicContext*> > >::erase(std::_Rb_tree_iterator<std::pair<uft::Value const, xpath::DynamicContext*> >)': xpath_context.cpp:(.text._ZNSt8_Rb_treeIN3uft5ValueESt4pairIKS1_PN5xpath14DynamicContextEESt10_Select1stIS7_ENS4_7Context12ValueCompareESaIS7_EE5eraseESt17_Rb_tree_iteratorIS7_E[std::_Rb_tree<uft::Value, std::pair<uft::Value const, xpath::DynamicContext*>, std::_Select1st<std::pair<uft::Value const, xpath::DynamicContext*> >, xpath::Context::ValueCompare, std::allocator<std::pair<uft::Value const, xpath::DynamicContext*> > >::erase(std::_Rb_tree_iterator<std::pair<uft::Value const, xpath::DynamicContext*> >)]+0x28): undefined reference tostd :: _ Rb_tree_rebalance_for_erase(std :: _ Rb_tree_node_base*,std :: _ Rb_tree_node_base&)'/ home/creen/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o):在函数std::_Rb_tree<uft::Value, std::pair<uft::Value const, xpath::DynamicContext*>, std::_Select1st<std::pair<uft::Value const, xpath::DynamicContext*> >, xpath::Context::ValueCompare, std::allocator<std::pair<uft::Value const, xpath::DynamicContext*> > >::_M_insert_(std::_Rb_tree_node_base const*, std::_Rb_tree_node_base const*, std::pair<uft::Value const, xpath::DynamicContext*> const&)': xpath_context.cpp:(.text._ZNSt8_Rb_treeIN3uft5ValueESt4pairIKS1_PN5xpath14DynamicContextEESt10_Select1stIS7_ENS4_7Context12ValueCompareESaIS7_EE10_M_insert_EPKSt18_Rb_tree_node_baseSG_RKS7_[std::_Rb_tree<uft::Value, std::pair<uft::Value const, xpath::DynamicContext*>, std::_Select1st<std::pair<uft::Value const, xpath::DynamicContext*> >, xpath::Context::ValueCompare, std::allocator<std::pair<uft::Value const, xpath::DynamicContext*> > >::_M_insert_(std::_Rb_tree_node_base const*, std::_Rb_tree_node_base const*, std::pair<uft::Value const, xpath::DynamicContext*> const&)]+0xc4): undefined reference tostd :: _ Rb_tree_insert_and_rebalance(bool,std :: _ Rb_tree_node_base*,std :: _ Rb_tree_node_base*,std :: _ Rb_tree_node_base&)'/ home/creen/Projects /Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o):在函数std::_Rb_tree_const_iterator<std::pair<uft::Value const, xpath::DynamicContext*> >::operator--()': xpath_context.cpp:(.text._ZNSt23_Rb_tree_const_iteratorISt4pairIKN3uft5ValueEPN5xpath14DynamicContextEEEmmEv[std::_Rb_tree_const_iterator<std::pair<uft::Value const, xpath::DynamicContext*> >::operator--()]+0x1c): undefined reference tostd :: _ Rb_tree_decrement(std :: _ Rb_tree_node_base const*)'/ home/creen/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o):在函数std::_Rb_tree_const_iterator<std::pair<uft::Value const, xpath::DynamicContext*> >::operator++()': xpath_context.cpp:(.text._ZNSt23_Rb_tree_const_iteratorISt4pairIKN3uft5ValueEPN5xpath14DynamicContextEEEppEv[std::_Rb_tree_const_iterator<std::pair<uft::Value const, xpath::DynamicContext*> >::operator++()]+0x1c): undefined reference tostd :: _ Rb_tree_increment(std :: _ Rb_tree_node_base const*)'/ home/creen/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/l ibxml.a(xpath_context.o):在函数__gnu_cxx::new_allocator<mdom::Node*>::allocate(unsigned int, void const*)': xpath_context.cpp:(.text._ZN9__gnu_cxx13new_allocatorIPN4mdom4NodeEE8allocateEjPKv[__gnu_cxx::new_allocator<mdom::Node*>::allocate(unsigned int, void const*)]+0x4c): undefined reference tostd :: __ throw_bad_alloc()'/ home/screen /Projects/Riidr /workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o)中:在函数__gnu_cxx::new_allocator<mdom::Node>::allocate(unsigned int, void const*)': xpath_context.cpp:(.text._ZN9__gnu_cxx13new_allocatorIN4mdom4NodeEE8allocateEjPKv[__gnu_cxx::new_allocator<mdom::Node>::allocate(unsigned int, void const*)]+0x4c): undefined reference tostd中: :__ throw_bad_alloc(std::_Rb_tree_iterator<std::pair<uft::Value const, xpath::DynamicContext*> >::operator--()': xpath_context.cpp:(.text._ZNSt17_Rb_tree_iteratorISt4pairIKN3uft5ValueEPN5xpath14DynamicContextEEEmmEv[std::_Rb_tree_iterator<std::pair<uft::Value const, xpath::DynamicContext*> >::operator--()]+0x1c): undefined reference to)'/ home/screen /Projects/Riidr /workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o):在函数std :: _ Rb_tree_decrement(std :: _ Rb_tree_node_base*)'/ home/creen/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o):在函数std::deque<mdom::Node, std::allocator<mdom::Node> >::_M_new_elements_at_front(unsigned int)': xpath_context.cpp:(.text._ZNSt5dequeIN4mdom4NodeESaIS1_EE24_M_new_elements_at_frontEj[std::deque<mdom::Node, std::allocator<mdom::Node> >::_M_new_elements_at_front(unsigned int)]+0x58): undefined reference tostd :: __ throw_length_error(char const*)'/ home/creen/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o):在函数std::deque<mdom::Node, std::allocator<mdom::Node> >::_M_new_elements_at_back(unsigned int)': xpath_context.cpp:(.text._ZNSt5dequeIN4mdom4NodeESaIS1_EE23_M_new_elements_at_backEj[std::deque<mdom::Node, std::allocator<mdom::Node> >::_M_new_elements_at_back(unsigned int)]+0x58): undefined reference tostd :: __ throw_length_error(char const*)'/home/screen/Project/Ricidr /workspace/RiidrEbookReader/obj/local/armeabi/libxml.a( xpath_context.o):在函数__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<uft::Value const, xpath::DynamicContext*> > >::allocate(unsigned int, void const*)': xpath_context.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt13_Rb_tree_nodeISt4pairIKN3uft5ValueEPN5xpath14DynamicContextEEEE8allocateEjPKv[__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<uft::Value const, xpath::DynamicContext*> > >::allocate(unsigned int, void const*)]+0x4c): undefined reference tostd :: __ throw_bad_alloc()'/ home/screen /Projects/Riidr /workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_yacc.o)中:在函数std::list<uft::Value*, std::allocator<uft::Value*> >::_M_insert(std::_List_iterator<uft::Value*>, uft::Value* const&)': xpath_yacc.cpp:(.text._ZNSt4listIPN3uft5ValueESaIS2_EE9_M_insertESt14_List_iteratorIS2_ERKS2_[std::list<uft::Value*, std::allocator<uft::Value*> >::_M_insert(std::_List_iterator<uft::Value*>, uft::Value* const&)]+0x3c): undefined reference tostd :: _ List_node_base :: hook(std :: _ List_node_base*)'/ home/creen/Projects/Riidr/workspace/Ri idrEbookReader/obj/local/armeabi/libxml.a(xpath_yacc.o):在函数__gnu_cxx::new_allocator<std::_List_node<uft::Value*> >::allocate(unsigned int, void const*)': xpath_yacc.cpp:(.text._ZN9__gnu_cxx13new_allocatorISt10_List_nodeIPN3uft5ValueEEE8allocateEjPKv[__gnu_cxx::new_allocator<std::_List_node<uft::Value*> >::allocate(unsigned int, void const*)]+0x4c): undefined reference tostd :: __ throw_bad_alloc()'collect2:ld返回1退出状态make:* [/ home/creen/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libriidr-jni.so]错误1

任何信息,问题或任何事实都非常感谢.

小智 31

添加行

LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
Run Code Online (Sandbox Code Playgroud)

到Android.mk文件,它应该工作.正如在ndk doc中所解释的那样,这是因为ndk执行调试检查,即使是为了避免运行时链接错误,并且对于某些动机,它在预构建的库中找不到正确的引用.希望它可以提供帮助.

  • 虽然使用LOCAL_ALLOW_UNDEFINED_SUMBOLS:= true将获得编译代码,但可能导致程序无法运行.它会在运行时导致库找不到错误.除非你确定这是你想要的,否则如果你确定带有符号的共享库将安装在设备上,我会建议只使用它. (12认同)

Mic*_*ael 3

您必须添加APP_STL := stlport_staticApplication.mk文件,而不是Android.mk.

编辑:据我所知,您还没有链接到 stlport 静态库。所以我认为你应该添加类似的-lstlport内容LOCAL_LDLIBS