Most Android device is 32bit machine. So, many application assumes that host machine is 32bit system.
And in general, there is no difference developing Android-natvie-library between 64bit and 32bit build machine.

But, here is usual step for developing Android-native-library.
(1) Developing and verifying code at build machine.
(2) Porting to NDK build system.

Most developers turns on full-warning-option at compile to detect bugs at early stage.
But, building and verifying codes assuming 32bit host machine at 64bit machine always issues type casting warning due to different type size.
Especially, between pointer and integer.

For example, many Android JAVA application uses int - jint - as a type to contain native pointer with assumption of 32bit-host-system.
Building this code at 64bit build system to verify code issues type casting warning, even if code itself is built perfectly at NDK build system.
And it is worse that this code doesn't work at 64bit Android host machine, even though it is not popular.

To reduce this warnings (for easy-verifying of library code at build machine), in my opinion, using long - jlong - instead of jint as a type for containing native pointer is better unless memory space is extremely critical.
And to make compiler be happy, using macro can be a good choice.
Here is example of macro for type-casting between pointer and integer - jlong.
(This sample works well without warning at 32bit/64bit build/host system).

#define ptr2jlong(v) ((jlong)((intptr_t)(v)))
#define jlong2ptr(v) ((void*)((intptr_t)(v)))

This is just simple example for portability issues.
Making portable code is always very difficult...

블로그 이미지


댓글을 달아 주세요

* common

+ '-rdynamic' is not supported.
  android dynamic linker allows 'undefined symbol' only for weak symbol.(See (*1))
+ 'tmpfile()' function always returns NULL. "No such file or directory!"
+ 'strtod()' doesn't support '0xXXXX' format.

* NDK 8

'regex.h' exists in 'include'. But function bodies are not in library.

* NDK 9

'pthread_rwlock_xxxx' is declared in 'pthread.h'. But body is missing in library.
'pthread_exit' is declared without 'noreturn' attribute.
'regex' functions doesn't work as it should be. (bug? or not-implemented yet?? I have no idea.)

to be continued...

===== Details =====

(*1) : '-rdynamic' and 'weak' symbol (Updated at 2011-Aug-29)

Here is sample code to for testing 'weak' symbol at Android.

[ main.c ]
#include <dlfcn.h>
#include <stdio.h>

int g_d;

main() {
    void* handle;
    void  (*prf)(void);

    g_d = 369;

    handle = dlopen("/data/", RTLD_LAZY);
    if (!handle) {
        printf("Fail to load library\n");

    prf = dlsym(handle, "print_g");
    if (NULL != dlerror()) {
        printf("Fail to get symbol print_g\n");

    printf("=== DONE! ===\n");

[ lib.c ]
#include <stdio.h>

extern int g_d __attribute__((weak));

print_g(void) {
    printf("==> %d\n", g_d);

[ ]
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := libgtst


include $(CLEAR_VARS)

----- Test Steps -----
* build above codes at Android NDK
adb push /data/
adb push test /data/test
adb shell /data/test
==> 369
=== DONE! ===

to be continued...

블로그 이미지


Tag ndk

댓글을 달아 주세요