11

I get a lot of undefined reference to ... errors when I compile my single c++ source-file using the ndk-build tool. For the record I use NDK r6 on a linux host system.

First of all I've been banging my head against this problem all day, so I am sorry if I am leaving something out or explaining myself a bit fast. Please ask me for any additional information.

My source-code has a lot of dependent libraries that I want to statically link to. I have defined all my dependent libs of different in my Android.mk file as follows:

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)

While I make sure that these are available in my jni/ folder as lib.a with my Android.mk and Application.mk files. (and my source file) Also I've added STL support by adding APP_STL := stlport_static to my Application.mk file. My single source file (riidr-jni.cpp) is at the moment:

#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 );
}

If I uncomment the dp::platformInit( dp::PI_DEFAULT ); line and replace it with std::string test = "asd"; I have no errors at all, this makes me believe that the and therefore STL lib is correctly imported and linked.

The last instructions of my Android.mk file is this

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)

The error I get when running the ndk-build tool is the following:

/home/creen/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 tostd::_Rb_tree_increment(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*> > >::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): 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 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): 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 tostd::_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 tostd::_Rb_tree_increment(std::_Rb_tree_node_base const*)' /home/creen/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 tostd::__throw_bad_alloc()' /home/creen/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 tostd::__throw_bad_alloc()' /home/creen/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 tostd::_Rb_tree_decrement(std::_Rb_tree_node_base*)' /home/creen/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o): In function 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): In function 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/creen/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_context.o): In function __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/creen/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_yacc.o): In function 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/RiidrEbookReader/obj/local/armeabi/libxml.a(xpath_yacc.o): In function __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 returned 1 exit status make: * [/home/creen/Projects/Riidr/workspace/RiidrEbookReader/obj/local/armeabi/libriidr-jni.so] Error 1

Any info, questions or anything really is highly appreciated.

1
  • The project was halted - I could not get any of the answers to solve my actual problem. But thanks to all for the answers. Commented Dec 13, 2012 at 17:30

5 Answers 5

32

Add the line

LOCAL_ALLOW_UNDEFINED_SYMBOLS := true

to the Android.mk file and it should work. As explained in the ndk doc, it is because the ndk performs a debug checking even for avoiding runtime linking error and for some motivations it doesn't find the correct references in a pre-built library. Hope it coulds help.

Sign up to request clarification or add additional context in comments.

1 Comment

Although using LOCAL_ALLOW_UNDEFINED_SUMBOLS := true will get the code to compile, it may result in the program not running. It will cause a library not found error at run time. Unless you are sure this is what you want I would recomend only using it if you are sure the shared library with the symbols will be installed on the device.
3

You had to add APP_STL := stlport_static to the Application.mk file, not Android.mk.

EDIT: As far as I can see you haven't linked against stlport static library. So I think you should add something like -lstlport to LOCAL_LDLIBS.

1 Comment

Yes sorry, my bad. I ment Application.mk. I have edited my post now, thanks.
2

As others had mentioned,

LOCAL_ALLOW_UNDEFINED_SYMBOLS := true

will not work at run time. Have a look at this:

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

I ran into the same problem with boost (I use a custom 1.42 version).

Using the brute force method to configure gnustdc++ fixed my issue. The important thing is to link against -lgnustl_static (or whatever stl lib you use) after your libraries.

Comments

0

The problem is that some functionalities are enabled by default while they may need some extra linking to other libraries , this functionalities are : - support loading MOD music via modplug . - support loading MOD music via mikmod - support loading MP3 music via SMPEG you should disable these options to build sdl2_mixer so go to "Android.mk" ( of SDL2_mixer) file and you will find :

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

change those to "false" and it will work for you as did with me !

NOTES :

 LOCAL_ALLOW_UNDEFINED_SYMBOLS := true

1- will cause a runtime error , because the symbol will still not be resolved at runtime

2- You won't be able to load mp3 files but you can covert them to "ogg" with almost the same size and quality , but you can find the perfect solution of your exact problem here Runtime linking error with SDL_Mixer and SMPEG2 on Android

Comments

-1

Do you looked at the bug #16627 ?

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

I'm running ndk-r8, but I'm still having those errors though.

1 Comment

The project was halted, but no I didn't at the moment. But thanks anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.