编译用于Android的busybox---步骤,问题和解决

本文详细介绍了在Android环境下编译busybox的过程,包括遇到的语法错误和链接错误,如subscripted value错误和找不到crtbegin_static.o、crtend_android.o的问题。解决方案涉及到交叉编译器的选择、sysroot的设置以及编译选项的调整。通过调整makefile,最终成功编译并测试了busybox的可用性。文章还探讨了arm-eabi-gcc和arm-linux-androideabi-gcc的区别,以及nostdlib和sysroot选项的作用。
摘要由CSDN通过智能技术生成

编译用于Androidbusybox

使用arm-eabi-gcc编译busybox,设置-I${NDK_USR}/include -L${NDK_USR}/lib,源文件中出现许多subscripted value is neither array nor pointer nor vector语法error,通不过;换arm-linux-androideabi-gcc设置好-I –L后编译,最后链接阶段报打不开或找不到crtbegin_static.ocrtend_android.o错误,但是这两个目标文件在lib中存在。

试图解决此问题,并回答或解释:

crtbegin_static.ocrtend_android.o是否可以通过给ld选项予以解决?

arm-eabi-gccarm-linux-androideabi-gcc(jni native used)EABI标准上的差异?

Toolchain本身生成时(--with-sysroot)选项的差异和用途区别?

编译jni native codenon-jni native code的步骤;

按照后面的文档所述,编译busybox是使用eabi还是androideabi好呢?

Lib.a for busybox是个什么东西?

Busybox自己带crt.ostartup code?

 

解决途径:

1.       We recommend developers to make themselves familiar with JNI concepts. Alsonote thatthe NDK is *not* a good way towrite non-JNI native code for theAndroid platform. ---- extracted from ndk/README.txt

1.       Search arm-eabi- & arm-linux-androideabi- PREFIX & --with-sysroot & SYSROOT in Android Makefiles.

2.       Read GCC manual – for some options

 

参考后面资料后继续编译:

menuconfig中设置使用arm-linux-androideabi-toolchain,设置sysrootjellybeantop/prebuilts/ndk/android-ndk-r7/platforms/android-14/arch-arm/(注意是usr目录的上级目录),其中为usr/includeusr/lib),此时不再需要-I-L选项。则去除某些找不到链接项的feature后,可以编译成功,大小为800k左右。

针对编译过程中的语法错误,可以通过查看命令文件名去掉对应目录和名称的feature;针对链接过程中无法解析的符号,主要是libbb中的,可以通过sourceinsight查看源文件搜索出引用的命令,去掉该feature

将编译出来的busybox可执行文件,adb push到手机/system/bbdir/目录,测试

./busybox --help可用;

使用命令/system/bbdir/busybox –install –s.安装(注意命令中的当前目录),测试ls –al可用,还有彩色高亮显示。测试其余命令,可用。

编译完成后浏览源文件目录,发现lib.a被拷贝到每个源码目录下。

Makefile文件中有nostadlib选项:

LD             = $(CC)-nostdlib

Makefile.flags文件中有--sysroot设置和SYSROOT环境变量输出:

ifneq ($(subst "",,$(CONFIG_SYSROOT)),)

CFLAGS += --sysroot=$(CONFIG_SYSROOT)

export SYSROOT=$(CONFIG_SYSROOT)

endif

 

一些问题的解释:

为什么使用arm-eabi-toolchain会频繁报subscripted value is neither array nor pointer nor vector,下面这段busybox/Makefile中的注释也许能回答.

## TODO:

## gcc version 4.4.0 20090506 (Red Hat 4.4.0-4) (GCC) is a PITA:

## const char *ptr; ... off_t v = *(off_t*)ptr; -> BOOM

## and no easy way to convince it to shut the hell up.

## We have a lot of such things all over the place.

## Classic *(off_t*)(void*)ptr does not work,

## and I am unwilling to do crazy gcc specific ({ void *ppp = ...; })

## stuff in macros. This would obfuscate the code too much.

## Maybe try __attribute__((__may_alias__))?

#CFLAGS += $(call cc-ifversion, -eq, 0404, -fno-strict-aliasing)

 

链接时很多feature使用的API找不到?

这是因为bionic c库实现的限制: The NDK only provides system headers for a very limited set of native APIs and libraries supported by the Android platform.

-nostdlib--with-sysroot的作用?

-nostdlib包含-nostartupfiles-nodefaultlibs两个选项;前者是_start标号所在,可能是crt.o?后者是通常是libgcc(注意这里所说的缺省库标准库不是有些人以讹传讹的C标准库),是实现_init, _fini, __main, _atexit _initarray _finiarray等等这些的编译器标准库.

-sysroot才是指定target系统标准头文件和库文件目录的选项.

Busybox本身有定制的startup code,且不去使用编译器标准库,故加-nostdlib链接选项. Busybox/Makefile中有$(busybox-init), $(busybox-main)和链接布局描述;-sysroot选项主要是指定bionic c头文件和库目录上级rootdir[/usr].

 

--with-sysroot选项的作用

 传给GCC--with-sysroot选项,--sysroot

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值