如何将libc添加到Android应用程序?

时间:2015-10-12 08:11:25

标签: android-ndk glibc libc bionic

我正在编写一个C代码,我希望使用ndk在android中运行。我在代码中有glibc库,据我所知很难直接移植。因此我尝试使用libc而不是github中的bionic存储库。在构建它时我遇到了很多错误。

任何人都可以解释如何在一步一步的过程中包含libc(或引导我到某些来源)。我搜索了很多,无法在任何地方找到教程。

这些是我得到的错误:

In file included from jni/libc/private/ScopedPthreadMutexLocker.h:22:0,
                     from jni/libc/bionic/malloc_debug_common.cpp:47:
    jni/libc/private/bionic_macros.h:27:31: warning: defaulted and deleted functions only available with -std=c++11 or -std=gnu++11 [enabled by default]
       TypeName(const TypeName&) = delete;      \
                                   ^
    jni/libc/private/ScopedPthreadMutexLocker.h:37:3: note: in expansion of macro 'DISALLOW_COPY_AND_ASSIGN'
       DISALLOW_COPY_AND_ASSIGN(ScopedPthreadMutexLocker);
       ^
    jni/libc/private/bionic_macros.h:28:37: warning: defaulted and deleted functions only available with -std=c++11 or -std=gnu++11 [enabled by default]
       void operator=(const TypeName&) = delete
                                         ^
    jni/libc/private/ScopedPthreadMutexLocker.h:37:3: note: in expansion of macro 'DISALLOW_COPY_AND_ASSIGN'
       DISALLOW_COPY_AND_ASSIGN(ScopedPthreadMutexLocker);
       ^
    In file included from jni/libc/bionic/jemalloc.h:20:0,
                     from jni/libc/bionic/malloc_debug_common.cpp:50:
    jni/libc/jemalloc/jemalloc.h:108:36: error: declaration of C function 'size_t malloc_usable_size(void*)' conflicts with
     #  define JEMALLOC_CXX_THROW throw()
                                        ^
    jni/libc/jemalloc/jemalloc.h:231:43: note: in expansion of macro 'JEMALLOC_CXX_THROW'
         JEMALLOC_USABLE_SIZE_CONST void *ptr) JEMALLOC_CXX_THROW;
                                               ^
    In file included from /home/yogi/android-ndk/platforms/android-21/arch-arm/usr/include/string.h:33:0,
                     from /home/yogi/android-ndk/platforms/android-21/arch-arm/usr/include/signal.h:35,
                     from /home/yogi/android-ndk/platforms/android-21/arch-arm/usr/include/pthread.h:33,
                     from jni/libc/bionic/malloc_debug_common.h:36,
                     from jni/libc/bionic/malloc_debug_common.cpp:40:
    /home/yogi/android-ndk/platforms/android-21/arch-arm/usr/include/malloc.h:36:15: error: previous declaration 'size_t malloc_usable_size(const void*)' here
     extern size_t malloc_usable_size(const void* p);
                   ^
    jni/libc/bionic/malloc_debug_common.cpp:51:27: error: 'je_calloc' was not declared in this scope
     #define Malloc(function)  je_ ## function
                               ^
    jni/libc/bionic/malloc_debug_common.cpp:67:3: note: in expansion of macro 'Malloc'
       Malloc(calloc),
       ^
    jni/libc/bionic/malloc_debug_common.cpp:51:27: error: 'je_free' was not declared in this scope
     #define Malloc(function)  je_ ## function
                               ^
    jni/libc/bionic/malloc_debug_common.cpp:68:3: note: in expansion of macro 'Malloc'
       Malloc(free),
       ^
    jni/libc/bionic/malloc_debug_common.cpp:51:27: error: 'je_malloc' was not declared in this scope
     #define Malloc(function)  je_ ## function
                               ^
    jni/libc/bionic/malloc_debug_common.cpp:70:3: note: in expansion of macro 'Malloc'
       Malloc(malloc),
       ^
    jni/libc/bionic/malloc_debug_common.cpp:51:27: error: 'je_malloc_usable_size' was not declared in this scope
     #define Malloc(function)  je_ ## function
                               ^
    jni/libc/bionic/malloc_debug_common.cpp:71:3: note: in expansion of macro 'Malloc'
       Malloc(malloc_usable_size),
       ^
    jni/libc/bionic/malloc_debug_common.cpp:51:27: error: 'je_posix_memalign' was not declared in this scope
     #define Malloc(function)  je_ ## function
                               ^
    jni/libc/bionic/malloc_debug_common.cpp:73:3: note: in expansion of macro 'Malloc'
       Malloc(posix_memalign),
       ^
    jni/libc/bionic/malloc_debug_common.cpp:51:27: error: 'je_realloc' was not declared in this scope
     #define Malloc(function)  je_ ## function
                               ^
    jni/libc/bionic/malloc_debug_common.cpp:77:3: note: in expansion of macro 'Malloc'
       Malloc(realloc),
       ^
    jni/libc/bionic/malloc_debug_common.cpp:51:27: error: 'je_valloc' was not declared in this scope
     #define Malloc(function)  je_ ## function
                               ^
    jni/libc/bionic/malloc_debug_common.cpp:79:3: note: in expansion of macro 'Malloc'
       Malloc(valloc),
       ^
    jni/libc/bionic/malloc_debug_common.cpp: In function 'void get_malloc_leak_info(uint8_t**, size_t*, size_t*, size_t*, size_t*)':
    jni/libc/bionic/malloc_debug_common.cpp:191:96: error: 'je_malloc' was not declared in this scope
       HashEntry** list = static_cast<HashEntry**>(Malloc(malloc)(sizeof(void*) * g_hash_table.count));
                                                                                                    ^
    jni/libc/bionic/malloc_debug_common.cpp:214:22: error: 'je_free' was not declared in this scope
         Malloc(free)(list);
                          ^
    jni/libc/bionic/malloc_debug_common.cpp:236:20: error: 'je_free' was not declared in this scope
       Malloc(free)(list);
                        ^
    jni/libc/bionic/malloc_debug_common.cpp: In function 'void free_malloc_leak_info(uint8_t*)':
    jni/libc/bionic/malloc_debug_common.cpp:240:20: error: 'je_free' was not declared in this scope
       Malloc(free)(info);
                        ^
    jni/libc/bionic/malloc_debug_common.cpp: In function 'size_t malloc_usable_size(const void*)':
    jni/libc/bionic/malloc_debug_common.cpp:262:53: error: declaration of C function 'size_t malloc_usable_size(const void*)' conflicts with
     extern "C" size_t malloc_usable_size(const void* mem) {
                                                         ^
    In file included from jni/libc/bionic/jemalloc.h:20:0,
                     from jni/libc/bionic/malloc_debug_common.cpp:50:
    jni/libc/jemalloc/jemalloc.h:70:33: error: previous declaration 'size_t malloc_usable_size(void*)' here
     #  define je_malloc_usable_size malloc_usable_size
                                     ^
    jni/libc/jemalloc/jemalloc.h:230:41: note: in expansion of macro 'je_malloc_usable_size'
     JEMALLOC_EXPORT size_t JEMALLOC_NOTHROW je_malloc_usable_size(
                                             ^
    make: *** [obj/local/armeabi-v7a/objs/c/bionic/malloc_debug_common.o] Error 1

如何解决? 谢谢!

1 个答案:

答案 0 :(得分:2)

  

我尝试使用libc代替github中的bionic存储库。

您似乎已将Bionic libc复制到您的项目中,并正在尝试构建它。

不要。作为ndk的一部分,已经已经提供了预建副本,而且不需要做任何特别的事情使用它:ndk提供的编译器会自动执行此操作。

您应该专注于构建应用程序,而不是复制随机libc变体并尝试构建它们。