1. Nimm jetzt an unserem Uhans - 3. ADVENT - Gewinnspiel teil - Alle Informationen findest Du hier!

Compling Native C using NDK

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

  1. sven-ola, 15.09.2009 #1
    sven-ola

    sven-ola Threadstarter Android-Hilfe.de Mitglied

    Beiträge:
    78
    Erhaltene Danke:
    22
    Registriert seit:
    31.07.2009
    Hey,

    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.

    Code:
    # Dynamic hello example for Android NDK
    # http://honeypod.blogspot.com/2007/12/dynamically-linked-hello-world-for.html
    
    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-
    endif
    
    CC = $(CROSS_COMPILE)gcc
    LD = $(CROSS_COMPILE)ld
    
    # bionic setbuf() segfaults, do not use
    CFLAGS +=  -D'setbuf(a,b)='
    CFLAGS +=  -DUSE_FPM
    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 \
        $^ \
        $(NDK_ARCH)/usr/lib/crtend_android.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 \
        $^ \
        $(NDK_ARCH)/usr/lib/crtend_android.o
    
    clean:
        rm -f hello-ld hello-gcc
        rm -f *.o
        rm -f *~
    
    // Sven-Ola
     

    Anhänge:

    chrisqn, therealhoudini, enjoy_android und eine weitere Person haben sich bedankt.
  2. chrisqn, 03.06.2010 #2
    chrisqn

    chrisqn Neuer Benutzer

    Beiträge:
    2
    Erhaltene Danke:
    0
    Registriert seit:
    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...
    chris
     

Diese Seite empfehlen