开发者

android-ndk gnustl_static exe not working

开发者 https://www.devze.com 2023-02-10 05:58 出处:网络
I can\'t seem to get the following trivial code to compile/link and the problem seems specific to std::wstring and the gnustl_static C++ library. Any help would be appreciated.

I can't seem to get the following trivial code to compile/link and the problem seems specific to std::wstring and the gnustl_static C++ library. Any help would be appreciated.

main.cpp file:

#include <string>
int main(void)
{
    std::wstring wtest(L"Test");
    return 0;
}

Application.mk file:

APP_CFLAGS += -fexceptions
APP_STL := gnustl_static

Android.mk file:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := TestWCharApp
LOCAL_CFLAGS := -D_GLIBCXX_USE_WCHAR_T
LOCAL_SRC_FILES := main.cpp
include $(BUILD_EXECUTABLE)

When attempting to link the above application using gnustl_static I get the following error message:

undefined reference to `std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::basic_string(wchar_t const*, std::allocator<wchar_t> const&)'

If I change APP_STL to stlport_static and define _STLP_HAS_WCHAR_T everything seems to compile/link/run fine. I verify it works by uploading the exe to the emulator and running it via the shell.

I'm going to need to use the gnustl implementation for c++ exception support otherwise I'd go with stlport_shared. Any clues as to why the above sample works for stlport_stat开发者_如何学Cic but not gnustl_static?


It's a problem with the file $NDK/sources/cxx-stl/gnu-libstdc++/Android.mk

Add the following line to that file:

LOCAL_MODULE_FILENAME := libstdc++


What's your target OS? According to this thread, gnustl_static doesn't support wchar_t prior to 2.3.


From the platforms\android-*\arch-arm\usr\include\wchar.h header file:

/* IMPORTANT: Any code that relies on wide character support is essentially
 *            non-portable and/or broken. the only reason this header exist
 *            is because I'm really a nice guy. However, I'm not nice enough
 *            to provide you with a real implementation. instead wchar_t == char
 *            and all wc functions are stubs to their "normal" equivalent...
 */

Funny though that running the following simple program in an android emulator show that wchar_t is 4 bytes.

#include <stdio.h>
int main(void)
{
    printf("Size of wchar is %d\n", sizeof(wchar_t));
    return 0;
}

Another thing to consider. The JNI bridge provides two useful ways to marshal string data. GetStringUTFChars (returns const char ) and GetStringChars (returns jchar). How many bytes do you think a jchar is defined as ... 2.


Make sure you run "ndk-build clean" and manually delete your libs/ and obj/ directories.

(Reference)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号