Android NDK 提供的交叉工具链手动编译源码

Makefile 如下
NDK_ROOT=/root/android-ndk-r7
TOOLCHAINS_ROOT=$(NDK_ROOT)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86
TOOLCHAINS_PREFIX=$(TOOLCHAINS_ROOT)/bin/arm-linux-androideabi
TOOLCHAINS_INCLUDE=$(TOOLCHAINS_ROOT)/lib/gcc/arm-linux-androideabi/4.4.3/include-fixed

PLATFORM_ROOT=$(NDK_ROOT)/platforms/android-9/arch-arm
PLATFORM_INCLUDE=$(PLATFORM_ROOT)/usr/include
PLATFORM_LIB=$(PLATFORM_ROOT)/usr/lib

FLAGS=-I$(TOOLCHAINS_INCLUDE) \
-I$(PLATFORM_INCLUDE) \
-L$(PLATFORM_LIB) \
-nostdlib \
-lgcc \
-Bdynamic \
-lc


OBJS=test.o \
$(PLATFORM_LIB)/crtbegin_dynamic.o \
$(PLATFORM_LIB)/crtend_android.o

all:
        rm -rf test.o
        rm -rf test
        $(TOOLCHAINS_PREFIX)-gcc $(FLAGS) -c test.c -o test.o
        $(TOOLCHAINS_PREFIX)-gcc $(FLAGS) $(OBJS) -o test

test.c
#include "stdio.h"
int main()
{
        printf("hello world\n");
        return 0;
}

说明:

Android 使用的是bionic的交叉工具链

Android 的交叉工具链c开工代码为:crtbegin_dynamic.o

Makefile 中的几个传递给连接器的参数不能少

-nostlib 不加会引起 crtbegin_dynamic.o: No such file: No such file or directory 错误

你可能感兴趣的:(c,android,File,工具,include,makefile)