2

我编写了一个简单的 C++ 测试程序来测试异常

#include <iostream>
#include <string>

inline bool isNumber(char c);

inline unsigned stringToNum(const char *str) {
    unsigned x = 0;
    const char *p = str;
    while(1) {
        if( !isNumber(*p) ) {  
            if( *p == 0 && p != str )
                break;
            throw 0;
        }
        x = x * 10 + *p++ - '0';
    }
    return x;
}

inline bool isNumber( char c ) {
    return c >= '0' && c <= '9';
}

int main() {
    std::string k = std::string("test");
    try {
        int p = stringToNum(k.c_str());
    } catch(...) {
        std::cout << "EXCEPTION THROWN";
    }
}

我分两个阶段编译程序(我试图模拟一个更大的库发生的错误)。第一步看起来像

/Users/me/android-ndk-r8/toolchains/x86-4.4.3/prebuilt/darwin-x86/bin/i686-android-linux-g++ -c -fexceptions -frtti -Wnon-virtual-dtor -Woverloaded-virtual --sysroot "/Users/me/android-ndk-r8/platforms/android-14/arch-x86/"  -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -O3 -Wl,-rpath-link=/Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/ -L/Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/ -nostdlib /Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/crtbegin_dynamic.o -lc -lm -I/Users/me/android-ndk-r8/sources/cxx-stl/gnu-libstdc++/include -I/Users/me/android-ndk-r8/sources/cxx-stl/gnu-libstdc++/libs/x86/include exceptiontest.cpp

接着:

/Users/me/android-ndk-r8/toolchains/x86-4.4.3/prebuilt/darwin-x86/bin/i686-android-linux-g++ -fexceptions -fPIC -pthread -rdynamic --sysroot "/Users/me/android-ndk-r8/platforms/android-14/arch-x86/" -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -O3 -Wl,-rpath-link=/Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/ -L/Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/ -nostdlib /Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/crtbegin_dynamic.o -lc -lm exceptiontest.o -L/Users/me/android-ndk-r8/toolchains/x86-4.4.3/prebuilt/darwin-x86/lib/gcc/i686-android-linux/4.4.3 -L/Users/me/android-ndk-r8/sources/cxx-stl/gnu-libstdc++/libs/x86 -lgnustl_shared -lgcc

当我在设备上运行 a.out 时(将 libgnustl_shared.so 复制到 libs 目录),我得到一个段错误。我错过了什么?编译器标志?图书馆?等等

编辑:继承gdb的segfault的堆栈跟踪

#0  get_cie_encoding (cie=0x6107daff) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2-fde.c:267
#1  0x080b16d8 in classify_object_over_fdes (ob=0x80d8b68, this_fde=0x80d0a1c) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2-fde.c:615
#2  0x080b1a5d in search_object (ob=0x80d8b68, pc=<value optimized out>) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2-fde.c:726
#3  0x080b1f84 in _Unwind_Find_FDE (pc=0x80b10da, bases=0xbfa61204) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2-fde.c:989
#4  0x080b07db in uw_frame_state_for (context=<value optimized out>, fs=0xbfa6102c) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2.c:1128
#5  0x080b0af8 in uw_init_context_1 (context=0xbfa61230, outer_cfa=<value optimized out>, outer_ra=0x809aa14) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2.c:1447
#6  0x080b10db in _Unwind_RaiseException (exc=0x944d060) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind.inc:88
#7  0x0809aa14 in __cxa_throw ()
#8  0x0804b4a7 in main () at exceptiontest.cpp:13
4

1 回答 1

0

正确答案:Android NDK R5 和对 C++ 异常的支持

不正确的旧答案(这不再是真的。感谢克里斯指出):

Android 平台不支持 C++ 中的异常。这在 Bionic 文档中有明确说明。此外,您可以在 android-ndk google 组上阅读此线程,以详细讨论该主题。

于 2012-06-19T16:47:27.887 回答