Ndk-build:未定义引用.静态链接到libxml.a时出错



我得到了很多未定义的引用…当我使用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)

当我确保这些在我的jni/文件夹中作为lib。a用我的安卓系统。mk和Application。可文件。(和我的源文件)此外,我通过将APP_STL := stlport_static添加到我的应用程序中来添加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 );
}

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

我的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)

当运行ndk-build工具时,我得到的错误如下:

/home/green/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o): In function 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 to std::_Rb_tree_increment(std::_Rb_tree_node_base*)'/home/green/projects/riidr/workspace/riidrebookreader/obj/local/armeabi/libxml.a(xpath_context.o): In function 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 to std::_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): In function 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 to std::_Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)'/home/green/projects/riidr/workspace/riidrebookreader/obj/local/armeabi/libxml.a(xpath_context.o): In function 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 to std::_Rb_tree_decrement(std::_Rb_tree_node_base const*)'/home/creen/projects/riidr/workspace/riidrebookreader/obj/local/armeabi/libxml.a(xpath_context.o): In function 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 to std::_Rb_tree_increment(std::_Rb_tree_node_base const*)'/home/screen/projects/riidr/workspace/riidrebookreader/obj/local/armeabi/libxml.a(xpath_context.o): In function __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 to std::__throw_bad_alloc()'/home/screen/projects/riidr/workspace/riidrebookreader/obj/local/armeabi/libxml.a(xpath_context.o): In function __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 to std::__throw_bad_alloc()'/home/green/projects/riidr/workspace/riidrebookreader/obj/local/armeabi/libxml.a(xpath_context.o): In function 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 std::_Rb_tree_decrement(std::_Rb_tree_node_base*)'/home/screen/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 to std::__throw_length_error(char const*)'/home/screen/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 to std::__throw_length_error(char const*)'/home/screen/projects/riidr/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 to std::__throw_bad_alloc()'/home/screen/projects/riidr/workspace/riidrebookreader/obj/local/armeabi/libxml.a(xpath_yaccc .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 to std::_List_node_base::hook(std::_List_node_base*)'/home/screen/projects/riidr/workspace/riidrebookreader/obj/local/armeabi/libxml.a(xpath_yaccc .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 to std::__throw_bad_alloc()'Collect2: ld返回1退出状态make: * [/home/green/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libriidr-jni. exe]错误1

任何信息,问题或任何事情都非常感谢。

添加行

LOCAL_ALLOW_UNDEFINED_SYMBOLS := true

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

您必须将APP_STL := stlport_static添加到Application.mk文件中,而不是Android.mk

EDIT:据我所知,你还没有链接到stlport静态库。所以我认为你应该在LOCAL_LDLIBS上加上-lstlport之类的东西

就像其他人提到的,

LOCAL_ALLOW_UNDEFINED_SYMBOLS:= true

在运行时不起作用。看看这个:

https://github.com/MysticTreeGames/Boost-for-Android

我在boost中遇到了同样的问题(我使用自定义的1.42版本)。

使用暴力方法配置gnustdc++修复了我的问题。重要的是在库之后链接到-lgnustl_static(或您使用的任何stl lib)。

问题是一些功能是默认启用的,而它们可能需要一些额外的链接到其他库,这些功能是:-支持加载MOD音乐通过modplug。-支持加载MOD音乐通过mikmod-支持通过SMPEG加载MP3音乐你应该禁用这些选项来构建sdl2_mixer,所以转到"Android"。mk" (of SDL2_mixer)文件,你会发现:

SUPPORT_MOD_MODPLUG ?= true
...
SUPPORT_MOD_MIKMOD ?= true
...
SUPPORT_MP3_SMPEG ?= true

将这些更改为"false",它将为您工作,就像我一样!

指出:

 LOCAL_ALLOW_UNDEFINED_SYMBOLS := true

1-将导致运行时错误,因为该符号在运行时仍未解析

2-你将无法加载mp3文件,但你可以转换为"ogg"几乎相同的大小和质量,但你可以找到你的确切问题的完美解决方案在这里运行时链接错误与SDL_Mixer和SMPEG2在Android

你看bug #16627吗?

http://code.google.com/p/android/issues/detail?id=16627

我正在运行ndk-r8,但我仍然有这些错误。

最新更新