Compling Native C using NDK

Dieses Thema im Forum "Android Codeschnipsel" wurde erstellt von sven-ola, 15.09.2009.

  1. #1 sven-ola, 15.09.2009

    didn't find too much info on the web on this, so I've investgated a bit on my own. Here's a sample of how to compile a native executable using the Android-NDK Version 1.5. You need a Linux for that with at least the NDK and the build-essentials installed.

    Note, that there's an oops with setbuf(). Expect others quirks while porting programs. I'll paste the Makefile here so google can index the critial keywors. Sample program in the attached ZIP.

    # Dynamic hello example for Android NDK
    NDK = /usr/src/android-ndk-1.5_r1
    NDK_ARCH = $(NDK)/build/platforms/android-1.5/arch-arm
    NDK_COMP = $(NDK)/build/prebuilt/linux-x86/arm-eabi-4.2.1
    ifneq ($(shell uname -m),armv6l)
    # When  not compiling on the phone use cross compiler
    CROSS_COMPILE = $(NDK_COMP)/bin/arm-eabi-
    CC = $(CROSS_COMPILE)gcc
    # bionic setbuf() segfaults, do not use
    CFLAGS +=  -D'setbuf(a,b)='
    CFLAGS += -I$(NDK_ARCH)/usr/include
    %o: %c Makefile
        $(CC) $(CFLAGS) -c $<
    all: hello-gcc hello-ld
    hello-gcc: hello.o fpm.o
        strace -o bla.txt -s 512 -f $(CC) -o $@ \
        -nostartfiles -nodefaultlibs -nostdlib \
            -lc -lm -lgcc -L$(NDK_ARCH)/usr/lib \
        -Wl,-rpath-link -Wl,$(NDK_ARCH)/usr/lib \
        -Wl,--dynamic-linker -Wl,/system/bin/linker \
        $(NDK_ARCH)/usr/lib/crtbegin_dynamic.o \
        $^ \
    hello-ld: hello.o fpm.o
        $(LD) -o $@ \
            -lc -lm -lgcc -L$(NDK_ARCH)/usr/lib \
        -L$(NDK_COMP)/lib/gcc/arm-eabi/4.2.1 \
        -rpath-link $(NDK_ARCH)/usr/lib \
        --dynamic-linker /system/bin/linker \
        $(NDK_ARCH)/usr/lib/crtbegin_dynamic.o \
        $^ \
        rm -f hello-ld hello-gcc
        rm -f *.o
        rm -f *~
    // Sven-Ola


    chrisqn, therealhoudini, enjoy_android und eine weitere Person haben sich bedankt.
  2. #2 chrisqn, 03.06.2010
    that MAKE file works great.
    its also possible to use it with eclipse.
    exactly what i was looking for

    thank you very much...

Diese Seite empfehlen