今天研究了一下busybox的编译。自己下了一个busybox-1.25.0的版本(直接从busybox官网上下载:https://busybox.net/downloads/),进行编译,遇到了一些问题,通过百度搜索和自己摸索,也成功解决了,详细记录如下:
首先交代一下系统版本和开发环境:
- 操作系统:ubuntu 12.04(64bit)
- 交叉编译工具链:arm-linux-gcc 4.4.3
- busybox源码包:busybox-1.25.0
一、修改Makefile配置
首先解压源码包:
tar -jxvf busybox-1.25.0.tar.bz2
- 1
进入busybox-1.25.0目录,修改Makefile文件如下:
ARCH ?= arm
CROSS_COMPILE ?= arm-linux-
- 1
- 2
二、修改配置文件
make menuconfig
- 1
选择Busybox Settings—>Build Options—>,选择[*] Build Busybox as a static binary(no shared libs);
三、编译源码,解决问题
make clean
make
- 1
- 2
出现如下错误:
miscutils/nandwrite.c: In function 'nandwrite_main':
miscutils/nandwrite.c:151: error: 'MTD_FILE_MODE_RAW' undeclared (first use in this function)
miscutils/nandwrite.c:151: error: (Each undeclared identifier is reported only once
miscutils/nandwrite.c:151: error: for each function it appears in.)
scripts/Makefile.build:197: recipe for target 'miscutils/nandwrite.o' failed
make[1]: *** [miscutils/nandwrite.o] Error 1
Makefile:742: recipe for target 'miscutils' failed
make: *** [miscutils] Error 2
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
解决办法:
MTD_FILE_MODE_RAW在/usr/include/mtd/mtd-abi.h中定义,于是将/usr/include/mtd/mtd-abi.h拷贝到busybox的include文件中,然后在nandwrite.c文件中包含该头文件:
gedit miscutils/nandwrite.c
- 1
修改
#include "libbb.h"
#include <mtd/mtd-user.h>
- 1
- 2
为
#include "libbb.h"
#include "mtd-abi.h"
#include <mtd/mtd-user.h>
- 1
- 2
- 3
此问题解决。继续make,又出现如下错误:
util-linux/blkdiscard.c: In function 'blkdiscard_main':
util-linux/blkdiscard.c:72: error: 'BLKSECDISCARD' undeclared (first use in this function)
util-linux/blkdiscard.c:72: error: (Each undeclared identifier is reported only once
util-linux/blkdiscard.c:72: error: for each function it appears in.)
scripts/Makefile.build:197: recipe for target 'util-linux/blkdiscard.o' failed
make[1]: *** [util-linux/blkdiscard.o] Error 1
Makefile:742: recipe for target 'util-linux' failed
make: *** [util-linux] Error 2
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
解决办法:
BLKSECDISCARD在/usr/include/linux/fs.h中定义,同上理,直接将/usr/include/linux/fs.h拷贝到busybox的include文件中,不过这次还要额外修改一下文件中的部分内容,否则会出现编译失败。
1、屏蔽以下4个头文件(否则编译时会提示找不到头文件):
//#include <linux/limits.h>
//#include <linux/ioctl.h>
//#include <linux/blk_types.h>
//#include <linux/types.h>
- 1
- 2
- 3
- 4
2、屏蔽以下几个结构体(否则编译时会提示找不到类型定义):
#if 0
struct fstrim_range {
__u64 start;
__u64 len;
__u64 minlen;
};
/* And dynamically-tunable limits and defaults: */
struct files_stat_struct {
unsigned long nr_files; /* read only */
unsigned long nr_free_files; /* read only */
unsigned long max_files; /* tunable */
};
struct inodes_stat_t {
int nr_inodes;
int nr_unused;
int dummy[5]; /* padding for sysctl ABI compatibility */
};
#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
然后修改blkdiscard.c中对fs.h头文件的包含方式:
gedit util-linux/blkdiscard.c
- 1
修改
#include <linux/fs.h>
- 1
为
#include "fs.h"
- 1
继续make,编译能通过了。但是在链接的时候出现问题:
networking/lib.a(nslookup.o): In function `print_host':
nslookup.c:(.text.print_host+0x44): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
debianutils/lib.a(mktemp.o): In function `mktemp_main':
mktemp.c:(.text.mktemp_main+0x98): warning: the use of `mktemp' is dangerous, better use `mkstemp'
networking/lib.a(ipcalc.o): In function `ipcalc_main':
ipcalc.c:(.text.ipcalc_main+0x25c): warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
libbb/lib.a(inet_common.o): In function `INET_resolve':
inet_common.c:(.text.INET_resolve+0x60): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(inetd.o): In function `reread_config_file':
inetd.c:(.text.reread_config_file+0x230): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(netstat.o): In function `ip_port_str':
netstat.c:(.text.ip_port_str+0x50): warning: Using 'getservbyport' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
util-linux/lib.a(nsenter.o): In function `nsenter_main':
nsenter.c:(.text.nsenter_main+0x1b0): undefined reference to `setns'
collect2: ld returned 1 exit status
Note: if build needs additional libraries, put them in CONFIG_EXTRA_LDLIBS.
Example: CONFIG_EXTRA_LDLIBS="pthread dl tirpc audit pam"
Makefile:717: recipe for target 'busybox_unstripped' failed
make: *** [busybox_unstripped] Error 1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
解决办法:
make menuconfig
- 1
Linux System Utilities—>nsenter,去掉该选项,重新编译make,又出现如下错误:
networking/lib.a(nslookup.o): In function `print_host':
nslookup.c:(.text.print_host+0x44): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
debianutils/lib.a(mktemp.o): In function `mktemp_main':
mktemp.c:(.text.mktemp_main+0x98): warning: the use of `mktemp' is dangerous, better use `mkstemp'
networking/lib.a(ipcalc.o): In function `ipcalc_main':
ipcalc.c:(.text.ipcalc_main+0x25c): warning: Using 'gethostbyaddr' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
libbb/lib.a(inet_common.o): In function `INET_resolve':
inet_common.c:(.text.INET_resolve+0x60): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(inetd.o): In function `reread_config_file':
inetd.c:(.text.reread_config_file+0x230): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
networking/lib.a(netstat.o): In function `ip_port_str':
netstat.c:(.text.ip_port_str+0x50): warning: Using 'getservbyport' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
coreutils/lib.a(sync.o): In function `sync_main':
sync.c:(.text.sync_main+0x7c): undefined reference to `syncfs'
collect2: ld returned 1 exit status
Note: if build needs additional libraries, put them in CONFIG_EXTRA_LDLIBS.
Example: CONFIG_EXTRA_LDLIBS="pthread dl tirpc audit pam"
Makefile:717: recipe for target 'busybox_unstripped' failed
make: *** [busybox_unstripped] Error 1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
解决办法:
make menuconfig
- 1
Coreutils—>sync选项去掉,重新make编译通过,终于顺利生成了busybox可执行文件。
OK,今天到此为止,明天再继续研究。
搞定,收工!
篇后语:
1、编译完后对上述几个问题进行反思,发现这几个问题其实来讲可以分为2类。一类是头文件包含的问题,一类是make menuconfig配置的问题。后者,可能跟每个人的配置需求有关。但是前者的问题,我认为busybox公司完全可以将那2个头文件直接包含在源码包中,将相关代码修改成OK的状态,让我们下载过来就可以直接编译通过,免去我们这番折腾。但是为何busybox公司没有这么做呢?其中应该是有原因的,暂时想不通,待日后再仔细琢磨吧。
2、本文撰写时,使用的busybox工具源码版本是busybox-1.25.0,使用上述方法验证通过。不过,紧接着,我又验证了最新的busybox-1.26.2版本,同样使用上述方法,顺利通过,编译成功。证明本文中的方法应该对于各个版本都是通用的。
3、本文只是简单的介绍了一下使用busybox过程中遇到几个编译出错的问题的解决办法,并未对整套编译流程进行详细介绍。关于如何利用busybox工具编译出linux根文件系统的完整流程,可参见我的另外一篇博客(如何使用busybox编译和生成最简linux根文件系统(rootfs))。
错误1
miscutils/i2c_tools.c: At top level:
miscutils/i2c_tools.c:1046: error: `I2C_FUNC_SMBUS_PEC‘ undeclared here (not in a function)
miscutils/i2c_tools.c:1046: error: initializer element is not constant
miscutils/i2c_tools.c:1046: error: (near initialization for `i2c_funcs_tab[12].value‘)
miscutils/i2c_tools.c:1047: error: initializer element is not constant
miscutils/i2c_tools.c:1047: error: (near initialization for `i2c_funcs_tab[12]‘)
miscutils/i2c_tools.c:1049: error: initializer element is not constant
miscutils/i2c_tools.c:1049: error: (near initialization for `i2c_funcs_tab[13]‘)
miscutils/i2c_tools.c:1051: error: initializer element is not constant
miscutils/i2c_tools.c:1051: error: (near initialization for `i2c_funcs_tab[14]‘)
miscutils/i2c_tools.c:1052: error: initializer element is not constant
miscutils/i2c_tools.c:1052: error: (near initialization for `i2c_funcs_tab[15]‘)
make[1]: *** [miscutils/i2c_tools.o] Error 1
make: *** [miscutils] Error 2
方法:
make menuconfig 去掉 i2c 相关模块
错误2
miscutils/ionice.c: In function `ioprio_set‘:
miscutils/ionice.c:23: error: `SYS_ioprio_set‘ undeclared (first use in this function)
miscutils/ionice.c:23: error: (Each undeclared identifier is reported only once
miscutils/ionice.c:23: error: for each function it appears in.)
miscutils/ionice.c: In function `ioprio_get‘:
miscutils/ionice.c:28: error: `SYS_ioprio_get‘ undeclared (first use in this function)
make[1]: *** [miscutils/ionice.o] Error 1
make: *** [miscutils] Error 2
方法:
make menuconfig 去掉 ionice 相关模块
错误3
miscutils/nandwrite.c:91: error: dereferencing pointer to incomplete type
miscutils/nandwrite.c:97: error: `MEMGETBADBLOCK‘ undeclared (first use in this function)
miscutils/nandwrite.c:97: error: (Each undeclared identifier is reported only once
miscutils/nandwrite.c:97: error: for each function it appears in.)
miscutils/nandwrite.c:102: error: dereferencing pointer to incomplete type
miscutils/nandwrite.c: In function `nandwrite_main‘:
miscutils/nandwrite.c:117: error: storage size of ‘meminfo‘ isn‘t known
miscutils/nandwrite.c:118: error: storage size of ‘oob‘ isn‘t known
miscutils/nandwrite.c:145: error: `MEMGETINFO‘ undeclared (first use in this function)
miscutils/nandwrite.c:252: error: `MEMREADOOB‘ undeclared (first use in this function)
miscutils/nandwrite.c:117: warning: unused variable `meminfo‘
miscutils/nandwrite.c:118: warning: unused variable `oob‘
make[1]: *** [miscutils/nandwrite.o] Error 1
方法:
make menuconfig 去掉 nandwrite,nanddump模块
错误4
出现如下错误:
miscutils/nandwrite.c: In function 'nandwrite_main':
miscutils/nandwrite.c:151: error: 'MTD_FILE_MODE_RAW' undeclared (first use in this function)
miscutils/nandwrite.c:151: error: (Each undeclared identifier is reported only once
miscutils/nandwrite.c:151: error: for each function it appears in.)
scripts/Makefile.build:197: recipe for target 'miscutils/nandwrite.o' failed
make[1]: *** [miscutils/nandwrite.o] Error 1
Makefile:742: recipe for target 'miscutils' failed
make: *** [miscutils] Error 2
解决办法:
MTD_FILE_MODE_RAW在/usr/include/mtd/mtd-abi.h中定义,直接在nandwrite.c中包含<mtd/mtd-abi.h>编译仍无法通过。
将/usr/include/mtd/mtd-abi.h拷贝到busybox的include文件中,然后包含该头文件:
#include "libbb.h"
#include "mtd-abi.h"
#include <mtd/mtd-user.h>
编译可以通过。或者直接在make menuconfig 去掉 nandwrite
错误4
miscutils/ubi_tools.c:67:26: mtd/ubi-user.h: No such file or directory
miscutils/ubi_tools.c: In function `ubi_tools_main‘:
miscutils/ubi_tools.c:106: error: `UBI_DEV_NUM_AUTO‘ undeclared (first use in this function)
miscutils/ubi_tools.c:106: error: (Each undeclared identifier is reported only once
miscutils/ubi_tools.c:106: error: for each function it appears in.)
miscutils/ubi_tools.c:107: error: `UBI_VOL_NUM_AUTO‘ undeclared (first use in this function)
miscutils/ubi_tools.c:115: error: field `attach_req‘ has incomplete type
miscutils/ubi_tools.c:116: error: field `mkvol_req‘ has incomplete type
miscutils/ubi_tools.c:117: error: field `rsvol_req‘ has incomplete type
miscutils/ubi_tools.c:181: error: `UBI_IOCATT‘ undeclared (first use in this function)
miscutils/ubi_tools.c:194: error: `UBI_IOCDET‘ undeclared (first use in this function)
miscutils/ubi_tools.c:237: error: `UBI_DYNAMIC_VOLUME‘ undeclared (first use in this function)
miscutils/ubi_tools.c:239: error: `UBI_STATIC_VOLUME‘ undeclared (first use in this function)
方法:
make menuconfig 去掉 ubi 相关模块
错误5
util-linux/umount.c: In function `umount_main‘:
util-linux/umount.c:87: error: `MNT_DETACH‘ undeclared (first use in this function)
util-linux/umount.c:87: error: (Each undeclared identifier is reported only once
util-linux/umount.c:87: error: for each function it appears in.)
make[1]: *** [util-linux/umount.o] Error 1
make: *** [util-linux] Error 2
方法:
umount 这里不能去掉了,因为这个功能是需要的 打开源码看看
83 // MNT_FORCE and MNT_DETACH (from linux/fs.h) must match
84 // OPT_FORCE and OPT_LAZY.
85 {
86 typedef char bug[
87 (OPT_FORCE != MNT_FORCE || OPT_LAZY != MNT_DETACH) ? -1 : 1
88 ];
打开 ARM GCC 的 include/linux/fs.h 确实没有发现这2个定义
从 编译环境中的linux 内核源码目录里面找到这个 include/linux/fs.h
如/usr/src/linux-headers-2.6.32-33/include/linux/fs.h
找到里面的这个定义
#define MNT_FORCE 0x00000001 /* Attempt to forcibily umount */
#define MNT_DETACH 0x00000002 /* Just detach from the tree */
所以我们直接把umount.c:这里的MNT_DETACH 直接写为2 在次 make
错误6
coreutils/lib.a(sync.o)(.text.sync_main+0x80): In function `sync_main‘:
: undefined reference to `syncfs‘
collect2: ld returned 1 exit status
make: *** [busybox_unstripped] Error 1
解决:
第一种方法:(简单)
通过find -name ”XXX” 查找定位错误文件所在目录。把这个目录里的Makefile或kbuild中的哪个报错的文件条件编译代码找到控制的编译宏名。然后在make menuconfig中把控制这个编译的宏选择不编译。
第二种方法: (复杂)
vi ./coreutils/sync.c
/*
* syncfs is documented to only fail with EBADF,
* which can‘t happen here. So, no error checks.
*/
86行 syncfs(fd);
18 行 //config: bool "Enable -d and -f flags (requres syncfs(2) in libc)"
找了一个 arm-libc 中并没有这个函数
下载了 busybox-1.23.0.tar.bz2 找到同名文件,发现这个内容明显要简单很多。用 1.23 的版本替换 1.24 的。
#include "libbb.h"
/* This is a NOFORK applet. Be very careful! */
int sync_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int sync_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
{
/* coreutils-6.9 compat */
bb_warn_ignoring_args(argv[1]);
sync();
return EXIT_SUCCESS;
}
在次编译通过了
Your linker does not support --sort-section,alignment
Trying libraries: crypt m
Library crypt is not needed, excluding it
Library m is needed, can‘t exclude it (yet)
Final link with: m