Android 编译 C 库

柴昆杰
2023-12-01

标准的做法是 把 src 放到  /android/develop/ 目录下去,然后建立 Android.mk 文件,

然后 make modue ...., 但是,这种方式 太麻烦。

 

下面介绍一种方法,可以比较快速的编译 C 库,

和上一篇中一样,首先要设置自己的环境变量:

 

 

cat setenv.sh

export PATH=$PATH:/android/myandroid/prebuilt/linux-x86/toolchain/arm-eabi-4.2.1/bin
export CROSS_COMPILER=arm-eabi
export TARGET=1
export ANDDROID=1
export A=/android/mydroid/

 

然后依然是建立一个 mk 文件:

 

cat zconfiglib.mk

 


ifdef ANDDROID
Abionic=$(A)bionic/libc/
AoutLib=$(A)out/target/product/teton_ebook/obj/lib/
CFLAGS += -I $(A)bionic/libc/arch-arm/include -I $(A)bionic/libc/include -I $(A)bionic/libc/kernel/common -I $(A)bionic/libc/kernel/arch-arm -c -fno-exceptions -Wno-multichar -march=armv5te -mtune=xscale -msoft-float -fpic -mthumb-interwork -ffunction-sections -funwind-tables /
 -fstack-protector -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ /
 -include $(A)system/core/include/arch/linux-arm/AndroidConfig.h -DANDROID -fmessage-length=0 -W -Wall -Wno-unused -DSK_RELEASE -DNDEBUG /
 -O2 -Wstrict-aliasing=2 -finline-functions -fno-inline-functions-called-once -fgcse-after-reload -frerun-cse-after-loop /
 -frename-registers -DNDEBUG -UDEBUG -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64  -MD

LIBS += -nostdlib -Wl,-soname, -Wl,-T,$(A)build/core/armelf.xsc -Wl,--gc-sections -Wl,-Bsymbolic -L$(AoutLib) -Wl,--whole-archive   -Wl,--no-whole-archive -lc  -lm -Wl,--no-undefined   $(A)/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0/bin/../lib/gcc/arm-eabi/4.4.0/interwork/libgcc.a

 

 

然后是你自己的 lib 的 makefile.

cat makefile

 


ifdef TARGET
include /zconfiglib.mk
CROSS=$(CROSS_COMPILER)-
CC=$(CROSS)gcc
AR=$(CROSS)ar
LD=$(CROSS)ld
else
CC = gcc
AR = ar
LD = ld
STRIP = strip
endif

INCLUDE+= -I../ -I./

LIBS+= -shared

OBJ_DIR = obj/
OBJECTS = $(OBJ_DIR)hello.o
TARGET_OUTPUT=./libhello

CFLAGS +=
all: $(TARGET_OUTPUT) $(TEST_OUTPUT) $(OBJECTS)

$(OBJ_DIR)%.o: %.c
        @-mkdir -p $(OBJ_DIR)
        $(CC) $(INCLUDE) $(CFLAGS) -c $< -o $@

$(TARGET_OUTPUT): $(OBJECTS)
        $(CC) $(LIBS) -o $(TARGET_OUTPUT) $(OBJECTS) $(LD_PATH)
        @echo "make $@ finished on `date`"
clean:
        @rm -f $(TARGET_OUTPUT)
        @rm -rf obj

 

然后  make 就可以了。


编译出来的库貌似需要放到  /system/lib 下去。

 类似资料: