Android NDK 콘솔용 Makefile

2009.11.12 19:11 from Android
안드로이드는 adb를 통해 shell 접근이 가능하다. NDK에서 사용하는 함수 확인이나 디버깅을 이렇게 shell에서 간단하게 확인해 보는것이 JNI를 통하는 것 보다 쉬울수도 있다.

Makefile
ANDROID_NDK=c:\andoird\android-ndk-1.6_r1

NDKROOT:=$(shell cygpath -u '$(ANDROID_NDK)')
SYSROOT:=$(NDKROOT)/build/platforms/android-4/arch-arm
TOOLCHAINDIR:=$(NDKROOT)/build/prebuilt/windows/arm-eabi-4.2.1/bin
TARGET_CC := $(TOOLCHAINDIR)/arm-eabi-gcc.exe
TARGET_LD := $(TOOLCHAINDIR)/arm-eabi-ld.exe

TARGET_LIBGCC := $(shell $(TARGET_CC) -mthumb-interwork -print-libgcc-file-name)
TARGET_LDLIBS := -Wl,-rpath-link=$(SYSROOT)/usr/lib $(TARGET_LIBGCC)
TARGET_CRTBEGIN_STATIC_O  := $(SYSROOT)/usr/lib/crtbegin_static.o
TARGET_CRTBEGIN_DYNAMIC_O := $(SYSROOT)/usr/lib/crtbegin_dynamic.o
TARGET_CRTEND_O := $(SYSROOT)/usr/lib/crtend_android.o
STDLIBS=c m stdc++
TARGET_SHARED_LIBRARIES:=$(foreach STDLIBS, $(STDLIBS), $(SYSROOT)/usr/lib/lib$(STDLIBS).so)
TARGET_STATIC_LIBRARIES:=$(foreach STDLIBS, $(STDLIBS), $(SYSROOT)/usr/lib/lib$(STDLIBS).a)
TARGET=test

INCLUDE:=-I$(SYSROOT)/usr/include
CFLAGS+=-c $(DEFINE) $(INCLUDE) \
    -march=armv5te -mtune=xscale \
    -msoft-float -fpic \
    -mthumb-interwork \
    -ffunction-sections \
    -funwind-tables \
    -fstack-protector \
    -fno-short-enums \
    -fshort-wchar \
    -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ \
    -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__
   
EXEFLAGS=-nostdlib -Bdynamic \
    -Wl,-dynamic-linker,/system/bin/linker \
    -Wl,--gc-sections \
    -Wl,-z,nocopyreloc \
    $(TARGET_SHARED_LIBRARIES) \
    $(TARGET_CRTBEGIN_DYNAMIC_O) \
    $(TARGET_STATIC_LIBRARIES) \
    $(TARGET_LDLIBS) \
    $(TARGET_CRTEND_O)   

all: $(TARGET)

$(TARGET): main.o
    $(TARGET_CC) $(EXEFLAGS) -o test main.o
   
clean:
    rm $(TARGET) main.o
   
main.o: main.c
    $(TARGET_CC) $(CFLAGS) -o $@ $<

main.c

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv)
{
    FILE* hFile = fopen("a.txt", "w");
   
    fprintf(hFile, "1234");
    fclose(hFile);
   
    return 0;
}

실행예
adb push test /data
adb shell
# cd /data
# chmod 777 test ; ./test
Posted by NOP 트랙백 0 : 댓글 1
NDK의 Makefile을 사용하지 않는 간단버전의 Makefile, target은 so파일

# tool chain setting
ANDROID_NDK16=c:\andoird\android-ndk-1.6_r1
NDKROOT:=$(shell cygpath -u '$(ANDROID_NDK16)')
SYSROOT:=$(NDKROOT)/build/platforms/android-4/arch-arm
TOOLCHAINDIR:=$(NDKROOT)/build/prebuilt/windows/arm-eabi-4.2.1/bin
CC:=$(TOOLCHAINDIR)/arm-eabi-gcc.exe
CXX:=$(TOOLCHAINDIR)/arm-eabi-g++.exe
AR:=$(TOOLCHAINDIR)/arm-eabi-ar.exe
LD:=$(TOOLCHAINDIR)/arm-eabi-ld.exe

TARGET_LIBGCC := $(shell $(CC) -mthumb-interwork -print-libgcc-file-name)
TARGET_LDLIBS := -Wl,-rpath-link=$(SYSROOT)/usr/lib $(TARGET_LIBGCC)
TARGET_CRTBEGIN_STATIC_O  := $(SYSROOT)/usr/lib/crtbegin_static.o
TARGET_CRTBEGIN_DYNAMIC_O := $(SYSROOT)/usr/lib/crtbegin_dynamic.o
TARGET_CRTEND_O := $(SYSROOT)/usr/lib/crtend_android.o
STDLIBS=c m stdc++
TARGET_SHARED_LIBRARIES:=$(foreach STDLIBS, $(STDLIBS), $(SYSROOT)/usr/lib/lib$(STDLIBS).so)
TARGET_STATIC_LIBRARIES:=$(foreach STDLIBS, $(STDLIBS), $(SYSROOT)/usr/lib/lib$(STDLIBS).a)
INCLUDE:=-I$(SYSROOT)/usr/include

CFLAGS+=-c $(DEFINE) $(INCLUDE) \
    -march=armv5te -mtune=xscale \
    -msoft-float -fpic \
    -mthumb-interwork \
    -ffunction-sections \
    -funwind-tables \
    -fstack-protector \
    -fno-short-enums \
    -fshort-wchar \
    -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ \
    -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__
ifeq ($(DEBUG),y)
    CFLAGS+= -O0 -g
else
    CFLAGS+= -O2
endif
LDFLAGS=-nostdlib -Wl,-soname,$(notdir $@) \
    -Wl,-shared,-Bsymbolic \
    -Wl,--no-whole-archive \
    $(TARGET_LDLIBS) \
    -Wl,--whole-archive -o

# source setting
EXCLUDES:=
SRCDIR:=../src \
    ../test/src
CSRCS:=$(foreach SRCDIR, $(SRCDIR), $(filter-out $(EXCLUDES), $(wildcard $(SRCDIR)/*.c)))
OBJS=$(CSRCS:.c=.o)
TARGET=libTest.so

INCLUDE+=
CFLAGS+=
LIBFILES+=

# label
%.o: %.c
    $(CC) $(CFLAGS) -o $@ $<

all: $(TARGET)
   
$(TARGET): $(OBJS)
        $(CC) $(LDFLAGS) $@ $(OBJS) $(LIBFILES)

clean:
    rm $(OBJS) $(TARGET)

Posted by NOP 트랙백 0 : 댓글 0
아래의 링크를 보면 xcode에서 Makefile을 이용한 빌드 방법이 소개 되어있다.
http://developer.apple.com/DOCUMENTATION/Porting/Conceptual/PortingUnix/preparing/chapter_3_section_4.html

설명된 내용과 같이 “External Target”과 “New Custom Executable”을 추가하면 무리없이 빌드가 가능하다. 그러나 디버깅시에는 디버깅 심볼을 추가함에도 Break point를 추가해도 Pause되지 않는다. 이런 경우에는 Preferences에서 Symbol Loading Options의 Load symbols lazily 옵션을 끄면 디버깅 할 수 있다.


하지만 Obj-C를 사용하는 것이 아니라면  그냥 Xcode를 사용하는 것 보다는 Eclipse를 사용하는 것이 더 편하다;;

Posted by NOP 트랙백 0 : 댓글 0