Here we are going to build an APP with NDK for Android, we will use Makefile during the building.
File --- Makefile
# --- Begin Customized ---
BIN = test
CUSTOM_OBJS = $(shell find . -name '*.c' | sed 's~.c$$~.o~g')
CUSTOM_CXXFLAGS = -Wall
CUSTOM_CFLAGS = -I.
CUSTOM_LDFLAGS = -L.
# --- End Customized ---
#NDK_ROOT=$(HOME)/tool/android-ndk-r9
NDK_ROOT=$(shell dirname $(shell which ndk-build))
TOOLCHAINS_ROOT=$(NDK_ROOT)/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64
TOOLCHAINS_PREFIX=$(TOOLCHAINS_ROOT)/bin/arm-linux-androideabi
TOOLCHAINS_INCLUDE=$(TOOLCHAINS_ROOT)/lib/gcc/arm-linux-androideabi/4.8/include-fixed
PLATFORM_ROOT=$(NDK_ROOT)/platforms/android-9/arch-arm
PLATFORM_INCLUDE=$(PLATFORM_ROOT)/usr/include
PLATFORM_LIB=$(PLATFORM_ROOT)/usr/lib
CFLAGS = -I$(TOOLCHAINS_INCLUDE) \
-I$(PLATFORM_INCLUDE) \
$(CUSTOM_CFLAGS)
CXXFLAGS = -Bdynamic \
$(CUSTOM_CXXFLAGS)
LDFLAGS = -L$(PLATFORM_LIB) \
-lgcc \
-lc \
-nostdlib \
$(CUSTOM_LDFLAGS)
OBJS = $(CUSTOM_OBJS) \
$(PLATFORM_LIB)/crtbegin_dynamic.o \
$(PLATFORM_LIB)/crtend_android.o
.PHONY: all clean
all: $(BIN)
$(BIN): $(OBJS)
$(TOOLCHAINS_PREFIX)-gcc $(CXXFLAGS) $(LDFLAGS) $^ -o $@
%.o: %.c
$(TOOLCHAINS_PREFIX)-gcc $(CFLAGS) $(CXXFLAGS) -c $^ -o $@
clean:
$(RM) $(BIN) $(CUSTOM_OBJS)
File --- test.c
#include "stdio.h"
int main()
{
printf("hello world\n");
return 0;
}
Building:
xiwang@ERIC-PC:~/localwork/android/hello-android-c$ make clean
rm -f test ./test.o
xiwang@ERIC-PC:~/localwork/android/hello-android-c$ make
/home/xiwang/tool/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc -I/home/xiwang/tool/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.8/include-fixed -I/home/xiwang/tool/android-ndk-r9/platforms/android-9/arch-arm/usr/include -I. -Bdynamic -Wall -c test.c -o test.o
/home/xiwang/tool/android-ndk-r9/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc -Bdynamic -Wall -L/home/xiwang/tool/android-ndk-r9/platforms/android-9/arch-arm/usr/lib -lgcc -lc -nostdlib -L. test.o /home/xiwang/tool/android-ndk-r9/platforms/android-9/arch-arm/usr/lib/crtbegin_dynamic.o /home/xiwang/tool/android-ndk-r9/platforms/android-9/arch-arm/usr/lib/crtend_android.o -o test
See:
xiwang@ERIC-PC:~/localwork/android/hello-android-c$ ls -ld test
-rwxrwxr-x 1 xiwang xiwang 6136 8月 8 23:10 test
FAQ:
What if need to add another header file as GCC option?
Just use option -include <header-file> to CC compile command. For example,
CFLAGS += -include /path/to/myheader.h