Android NDK 提供的交叉工具鏈手動編譯原始碼

LiuYinChina發表於2013-09-14

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 錯誤


相關文章