当我没有插入一些代码时,为什么ndk的“stlport”链接错误?

时间:2014-11-06 01:12:00

标签: android-ndk

我遇到了一些关于ndk的stlport链接的奇怪情况。 我的代码如下:

#include <cstdlib>
#include "igslib_imodule_manager.h"
#include "serial_manager_sample.h"
#include "rtc_sample.h"

int main(int argc,char** argv)
{
    igslib::IModuleManager* mm = GetModuleManagerInstance();
    int rtn = EXIT_SUCCESS;
    //igslib::serial_manager_sample::UartSend(mm);
    //igslib::serial_manager_sample::UartReceive(mm);
    //igslib::rtc_sample::GetTime(mm);
    return rtn;
}

这将导致stlport链接错误,如下所示:

naivechou@~/project/igslib/google_code/build/components_sample/jni>ndk-build
Android NDK: WARNING:/cygdrive/c/project/igslib/google_code/build/components_sample/jni/Android.mk:components_sample: non-system libraries in linker flags: -ligslib_module_manager
Android NDK:     This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES
Android NDK:     or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the
Android NDK:     current module
[armeabi] Compile++ thumb: components_sample <= main.cpp
[armeabi] Executable     : components_sample
C:/bin/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ../../../out/libigslib_module_manager.a(igslib_module_manager.o): in function std::basic_streambuf<char, std::char_traits<char> >::~basic_streambuf():C:/bin/android-ndk-r10c/sources/cxx-stl/stlport/stlport/stl/_streambuf.c:41: error: undefined reference to 'std::locale::~locale()'
C:/bin/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ../../../out/libigslib_module_manager.a(igslib_module_manager.o): in function std::basic_ios<char, std::char_traits<char> >::~basic_ios():C:/bin/android-ndk-r10c/sources/cxx-stl/stlport/stlport/stl/_ios.h:59: error: undefined reference to 'std::ios_base::~ios_base()'
C:/bin/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ../../../out/libigslib_module_manager.a(igslib_module_manager.o): in function std::__node_alloc::allocate(unsigned int&):C:/bin/android-ndk-r10c/sources/cxx-stl/stlport/stlport/stl/_alloc.h:158: error: undefined reference to 'std::__node_alloc::_M_allocate(unsigned int&)'
C:/bin/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ../../../out/libigslib_module_manager.a(igslib_module_manager.o): in function std::__node_alloc::deallocate(void*, unsigned int):C:/bin/android-ndk-r10c/sources/cxx-stl/stlport/stlport/stl/_alloc.h:161: error: undefined reference to 'std::__node_alloc::_M_deallocate(void*, unsigned int)'
C:/bin/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ../../../out/libigslib_module_manager.a(igslib_module_manager.o): in function igslib::CModuleManager::GetInstance():C:/project/igslib/google_code/build/module_manager/jni/../../../src/module_manager/igslib_module_manager.cpp:19: error: undefined reference to '__cxa_end_cleanup'
C:/bin/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ../../../out/libigslib_module_manager.a(igslib_module_manager.o):(.ARM.extab.text._ZN6igslib14CModuleManager11GetInstanceEv+0x0): error: undefined reference to '__gxx_personality_v0'
C:/bin/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ../../../out/libigslib_module_manager.a(igslib_module_manager.o): in function std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::~basic_stringbuf():C:/bin/android-ndk-r10c/sources/cxx-stl/stlport/stlport/stl/_sstream.c:59: error: undefined reference to '__cxa_end_cleanup'
C:/bin/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ../../../out/libigslib_module_manager.a(igslib_module_manager.o):(.ARM.extab.text._ZNSt15basic_stringbufIcSt11char_traitsIcESaIcEED2Ev+0x0): error: undefined reference to '__gxx_personality_v0'
C:/bin/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ../../../out/libigslib_module_manager.a(igslib_module_manager.o): in function std::pair<std::string const, igslib::CModuleManager::SModuleInfo>::~pair():C:/bin/android-ndk-r10c/sources/cxx-stl/stlport/stlport/stl/_string_base.h:156: error: undefined reference to '__cxa_end_cleanup'
C:/bin/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ../../../out/libigslib_module_manager.a(igslib_module_manager.o):(.ARM.extab.text._ZNSt4pairIKSsN6igslib14CModuleManager11SModuleInfoEED2Ev+0x0): error: undefined reference to '__gxx_personality_v0'
C:/bin/android-ndk-r10c/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ../../../out/libigslib_module_manager.a(igslib_module_manager.o): in function std::vector<igslib::CModuleManager::SModuleInfo, std::allocator<igslib::CModuleManager::SModuleInfo> >::~vector():C:/bin/android-ndk-r10c/sources/cxx-stl/stlport/stlport/stl/_alloc.h:323: error: undefined reference to '__cxa_end_cleanup'
...(After the text ellipsis)...

但如果我取消注释第一行igslib::serial_manager_sample::UartSend(mm);,一切都很好:

naivechou@~/project/igslib/google_code/build/components_sample/jni>ndk-build
Android NDK: WARNING:/cygdrive/c/project/igslib/google_code/build/components_sample/jni/Android.mk:components_sample: non-system libraries in linker flags: -ligslib_module_manager
Android NDK:     This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES
Android NDK:     or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the
Android NDK:     current module
[armeabi] Compile++ thumb: components_sample <= main.cpp
[armeabi] Executable     : components_sample
[armeabi] Install        : components_sample => libs/armeabi/components_sample
[custom install path]: ../../../out

我无法弄清楚会发生什么?为什么我插入代码会引发错误?

1 个答案:

答案 0 :(得分:3)

如果没有看到你的Application.mk和Android.mk,很难说,但看起来你正在使用LOCAL_LDLIBS列出igslib_module_manager库作为你的可执行文件的依赖。

这不是一个好主意,因为当你这样做时,ndk-build不知道这个库自己的依赖关系,并且最终的链接命令不正确(Unix链接工作的方式非常复杂,但解释了行为你看到了)。这就是为什么ndk-build总是抱怨警告。

您应该为igslib_module_manager定义一个模块,即使该库是预构建的,也可以使用LOCAL_STATIC_LIBRARIES + = igslib_module_manager代替您的Android.mk。

作为旁注,解释该行“解决”该问题的原因是因为这在“main.cpp”的生成对象文件中引入了额外的依赖性,最终将适当数量的STLport拉入到最终链接命令。但这真的只是偶然,你应该正确地列出你的依赖关系,以确保每次都能正常工作。