Compling Native C using NDK

S

sven-ola

Ambitioniertes Mitglied
24
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

  • android-dynamic.zip
    12,3 KB · Aufrufe: 349
  • Danke
Reaktionen: chrisqn, therealhoudini, enjoy_android und eine weitere Person
that MAKE file works great.
its also possible to use it with eclipse.
exactly what i was looking for

thank you very much...
chris
 

Ähnliche Themen

T
Antworten
3
Aufrufe
1.066
jogimuc
J
F
Antworten
1
Aufrufe
2.233
jogimuc
J
Zurück
Oben Unten