本博客倡导开放源代码,在此公布之程序源代码如无特别声明均采用GNU通用公共 许可证(GPL)

乐在其中

分享学习Linux的乐趣

  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  23 随笔 :: 0 文章 :: 401 评论 :: 0 Trackbacks
Buildroot大大简化了交叉编译工具链的制作,并且能为目标平台产生完整的根文件系统,还可以制作在目标平台上运行的本机编译(Native Compile)工具链。

我花了大量的时间研究buildroot,基于2009.11的版本做了不少修改,使得它符合固件开发的要求。下面列出的是一些主要的改动:
  • 增加了对2.6.12内核头文件的支持(编译uClibc需要)
  • 增加了对uClibc-0.9.28的支持
  • 增加了对busybox-1.15.3的支持
  • 增加了对gcc 3.4.4和gcc 4.4.2的支持
  • 增加了对nfs-utils-1.1.1的支持
  • 为libiconv瘦身,只保留了unicode和简体中文的支持,编译出的动态库从900多K减小到200多K。(编译samba需要libiconv)
  • 让libcurl和openssl只产生静态库。(编译transmission要用到)
1. 准备编译环境(适用于Ubuntu或Debian。我用的是Ubuntu 9.10)
安装编译所需软件包:
$ sudo apt
-get install build-essential libncurses5-dev bison flex texinfo zlib1g-dev gettext libssl-dev autoconf

buildroot用wget下载软件包源码,因此需要安装wget:
$ sudo apt-get install wget


Ubuntu默认的shell是dash,它跟buildroot的脚本有些不兼容。用下面的命令切换到bash:
$ sudo dpkg
-reconfigure dash

2. 下载buildroot-2009.11,放在HOME目录下,当然也可以放在其它目录。
$ cd
$ wget http:
//buildroot.net/downloads/buildroot-2009.11.tar.bz2
$ tar jxf buildroot-2009.11.tar.bz2

3. 打补丁。我把我的所有修改都放在一个补丁里:buildroot-2009.11.patch.zip
$ cd buildroot-2009.11
$ patch 
-p1 < ../buildroot-2009.11.patch

4. 配置buildroot。buildroot采用了和Linux内核一样的配置工具,使用非常方便。建议第一次编译时只选择生成工具链,暂时不要选择额外的软件包。我的补丁已经设置好了适合于固件开发默认配置,因此你做配置时可以全部接受默认值。(C++默认是关闭的,如果需要可以打开)
$ make menuconfig

屏幕截图 - make menuconfig:
screenshot
退出配置时会生成一个“.config”文件。

5. 编译buildroot。这可能会花几个小时,取决于你的Internet带宽以及CPU性能
$ make

编译好的工具链默认放在output/staging目录下,你可以在配置buildroot是指定到其它目录。我喜欢把它设为/opt/cross/gcc442-uc0928,表示gcc版本是4.4.2,uClibc版本是0.9.28。注意:指定的目录你一定要有写权限。如何设置:make menuconfig  --> Build Options --> Toolchain and header file location

屏幕截图 - 设置工具链位置:
screenshot

如果成功编译了工具链,接下来可以重新用make menuconfig选择所需软件包,比如我一般会选择nfs-utils, portmap, wireless-tool, wpa-supplicant,ipkg等等。选好软件包再次执行make。编译好的软件包会放在output/target目录下,这个目录拥有根文件系统的目录结构。

如果想要制作在目标平台运行的本机编译(Native Compile)工具链,则在make menuconfig时打开下面两个选项:
1. Build options子菜单下的development files in target filesystem

屏幕截图 - development files in target filesystem:
screenshot

2. Package Selection for the target子菜单下的native toolchain in the target filesystem

屏幕截图 - native toolchain in the target filesystem
screenshot

当然,为了能在目标平台上进行本机编译,还需要在配置时选择一些必要的编译工具以及程序库,如: make, autoconf, automake, bison, m4, flex, libz, libiconv 等等。

注:所谓本机编译(Native Compile),是相对于交叉编译而言的。其实这是最常见的编译方式,即在本机编译程序用于在本机或与本机兼容的硬件平台上运行。

补充:
1. autoconf, automake等软件包依赖于microperl软件包。在编译microperl时可能会遇到找不到m4的错误,这时只须执行make host-m4即可产生buildroot_dir/output/host/usr/bin/m4,然后再执行make,中断的编译将继续进行。
2. 编译某些软件包需要pkg-config for host(在编译平台上运行而非在目标平台上运行),执行make host-pkgconfig可编译产生buildroot_dir/output/host/usr/bin/pkg-config
posted on 2010-09-29 11:08 gouzhuang 阅读(28603) 评论(36)  编辑 收藏 引用 所属分类: 嵌入式Linux

评论

# re: Buildroot - 让交叉编译更轻松[未登录] 2010-04-29 00:47 aaron
http://buildroot.net/downloads/sources/ 这里uClibc0.9.28没有了,只有uClibc0.9.28.3  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-04-29 07:34 焦路
谢谢lz的文章。
原来没有用过linux。现在正准备装一个linux系统来开发固件。。
请问固件开发可不可以用c++。。
lz使用什么ide?  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-04-29 08:28 gouzhuang
@aaron
>http://buildroot.net/downloads/sources/ 这里uClibc0.9.28没有了,只有uClibc0.9.28.3
这里还有 http://www.uclibc.org/downloads/old-releases/  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-04-29 08:33 gouzhuang
@焦路
>请问固件开发可不可以用c++
当然可以,只是c++编译出来的执行文件稍大一点,依赖更多的动态库。
>lz使用什么ide?
目前主要是一些修修补补,没有用ide。  回复  更多评论
  

# 求助[未登录] 2010-04-30 19:20 aaron
楼主,偶买了ndas硬盘盒(http://code.ximeta.com/trac-ndas/wiki/OpenWRT),根据您的办法编译驱动程序失败(关键是这个参数不知道如何设置export NDAS_KERNEL_PATH=/),请指教一下啊,谢谢了!  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-05-03 11:58 gouzhuang
@aaron
>关键是这个参数不知道如何设置export NDAS_KERNEL_PATH=/
这个参数应该设置为OpenWRT的内核源码路径  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-05-04 11:42 丽可酷
原来没有用过linux  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-06-12 11:49 快播
可以尝试下  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-07-21 15:02 Michael
感谢LZ贡献  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-08-29 17:41 esmall
在下刚刚接触这方面,请问博主有没有好点的反汇编调试工具呢?
当然最好能在windows下对高清播放机进行真机调试的。
国内有没有专门讨论高清播放机编程的论坛呢?
  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-09-14 17:49 bgs90
build最新版本buildroot-2010.08,请问要打patch吗?  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-09-15 08:55 gouzhuang
@bgs90
buildroot各版本之间变化比较大,我的补丁几乎可以肯定不适用于新版本。不过我觉得2009.11版已经够用了。  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-07 18:46 hibaby
在这个环境下能帮我编译一下lftp4.0.10试试吗?
要先安装readline工具,另外,没有找到gnutls相关库,编译的时候需要--without-gnutls,还有要修改gettimeofday.c,函数重复定义了。
不过我做了上述修改后,编译的时候还是有问题,在编译FileAccess.o时,系统停住了,不知道出什么问题了。
不知道有没有时间在你的环境下试试看。
  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-19 14:16 bgs90@126.com
你好,我建立好了环境,打算修改一下transmission软件,重新编译一下,请问怎么弄啊?  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-20 12:24 gouzhuang
@bgs90@126.com
我是用下面的命令来配置transmission的:

CFLAGS='-Os -pipe -I/opt/cross/gcc442-uc0928/usr/include -mtune=mips32 -mabi=32 -msoft-float -static-libgcc -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' \
PKG_CONFIG='/soft/build/buildroot-2009.11/output/host/usr/bin/pkg-config' \
LDFLAGS='-L/opt/cross/gcc442-uc0928/lib -L/opt/cross/gcc442-uc0928/usr/lib' \
./configure --host=mipsel-linux --prefix=/usr --disable-gtk --disable-nls CPPFLAGS=-DTR_EMBEDDED

/opt/cross/gcc442-uc0928是我的工具链安装地址,/soft/build/buildroot-2009.11 是buildroot目录,请用你的路径替换。

configure完成了执行make进行编译。  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-20 14:16 bgs90@126.com
你好,按照你的要求,我先下载了最新的transmission 2.11
wget http://download.m0k.org/transmission/files/transmission-2.11.tar.bz2

解压后,到目录下,执行你的配置命令,除了buildroot目录是/home/allen/前缀外,其他配置跟你完全相同。

但是执行configure的时候报错:是transmission版本不对还是其他问题?

$ CFLAGS='-Os -pipe -I/opt/cross/gcc442-uc0928/usr/include -mtune=mips32 -mabi=32 -msoft-float -static-libgcc -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' PKG_CONFIG='/home/allen/buildroot-2009.11/output/host/usr/bin/pkg-config' LDFLAGS='-L/opt/cross/gcc442-uc0928/lib -L/opt/cross/gcc442-uc0928/usr/lib' ./configure --host=mipsel-linux --prefix=/usr --disable-gtk --disable-nls CPPFLAGS=-DTR_EMBEDDED
configure: WARNING: If you wanted to set the --build type, don't use --host.
If a cross compiler is detected then cross compile mode will be used.
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for mipsel-linux-strip... no
checking for strip... strip
checking for a thread-safe mkdir -p... /bin/mkdir -p
checking for gawk... no
checking for mawk... mawk
checking whether make sets $(MAKE)... yes
checking how to create a ustar tar archive... gnutar
checking build system type... i686-pc-linux-gnu
checking host system type... mipsel-unknown-linux-gnu
checking for style of include used by make... GNU
checking for mipsel-linux-gcc... no
checking for gcc... gcc
checking whether the C compiler works... no
configure: error: in `/home/allen/buildroot-2009.11/transmission/transmission-2.11':
configure: error: C compiler cannot create executables
See `config.log' for more details.  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-20 14:43 gouzhuang
@bgs90@126.com

>>checking for mipsel-linux-gcc... no

找不到mipsel-linux-gcc。你把/opt/cross/gcc442-uc0928/usr/bin加到你的PATH了吗?  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-20 17:14 bgs90@126.com
谢谢博主啊,加了PATH后,编译又缺少pkg-config,然后重新在工具链中吧pkg-config编译了进去,回过头来编译transmission时,又报错了,这次configure的时候是:

checking for library containing socket... none required
checking for library containing gethostbyname... none required
checking pkg-config is at least version 0.9.0... yes
checking for OPENSSL... no
checking for OpenSSL... /usr
checking for LIBCURL... configure: error: Package requirements (libcurl >= 7.15.4) were not met:

No package 'libcurl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBCURL_CFLAGS
and LIBCURL_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.


------------------
找不到libcurl,这个我看了一下在buildroot-2009.11/package目录下,但是不知道怎么设LIBCURL_CFLAGS,LIBCURL_LIBS变量啊?也不知道是否后面还有这些问题,博主有没有qq/MSN请教一下啊?我是17828柒446
  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-22 13:36 gouzhuang
@bgs90@126.com
请检查buildroot-2009.11/output/host/usr/bin目录下是否有pkg-config。如果没有执行make host-pkgconfig编译pkg-config,然后再尝试配置transmission

另外,我原来编译的是transmission-1.92,而2.11版增加了对libevent的依赖,因此你还需要在配置buildroot时选择libevent软件包。  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-22 16:19 bgs90@126.com
pkg-config已经进去了,增加了libevent的编译,但是还是libcurl的报错啊。  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-22 16:41 bgs90@126.com
另外,我下载了1.92版本,报的是同样的错误.
我是从http://download.m0k.org/transmission/files/
下载的  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-23 15:39 gouzhuang
@bgs90@126.com
你确认buildroot把libcurl编进来了吗?  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-26 09:31 bgs90@126.com
@gouzhuang
你确认buildroot把libcurl编进来了吗?

---------不知道啊,我在编译buildroot的时候make menuconfig里面找了半天没找到libcurl在哪里啊。  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-26 09:50 gouzhuang
@bgs90@126.com
Package Selection for the target ---> Networking ---> libcurl  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-26 16:21 bgs90@126.com
疯了,还是没搞定,编译transmission 2.04版本(其他版本也一样),报错。
configure结果如下:
CFLAGS='-Os -pipe -I/opt/cross/gcc442-uc0928/usr/include -mtune=mips32 -mabi=32 -msoft-float -static-libgcc -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' \
PKG_CONFIG='/home/allen/buildroot-2009.11/output/host/usr/bin/pkg-config' \
LDFLAGS='-L/opt/cross/gcc442-uc0928/lib -L/opt/cross/gcc442-uc0928/usr/lib' \
./configure --host=mipsel-linux --prefix=/usr --disable-gtk --disable-nls CPPFLAGS=-DTR_EMBEDDED

..............
Configuration:

Source code location: .
Compiler: g++

Build Command-Line client: yes

Build GTK+ client: no

Optional dependencies for GTK+ client:

* dbus support: no
* gio for watchdir support: no
* libnotify for 'download completed' popups: no
* libcanberra for 'download completed' sounds: no
* gconf2 to register as a magnet link handler: no
* libappindicator for an Ubuntu-style tray: no

Build Daemon: yes

Build Mac client: no

  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-26 16:22 bgs90@126.com
下面执行make
。。。。。。。。。
make[2]: Leaving directory `/home/allen/buildroot-2009.11/transmission/transmission-2.04/third-party/dht'
make[2]: Entering directory `/home/allen/buildroot-2009.11/transmission/transmission-2.04/third-party'
make[2]: Nothing to be done for `all-am'.
make[2]: Leaving directory `/home/allen/buildroot-2009.11/transmission/transmission-2.04/third-party'
make[1]: Leaving directory `/home/allen/buildroot-2009.11/transmission/transmission-2.04/third-party'
Making all in libtransmission
make[1]: Entering directory `/home/allen/buildroot-2009.11/transmission/transmission-2.04/libtransmission'
CC announcer.o
In file included from utils.h:18,
from crypto.h:22,
from announcer.c:21:
/usr/include/stdio.h:425: warning: redundant redeclaration of 'fscanf'
/usr/include/stdio.h:428: warning: redundant redeclaration of 'scanf'
/usr/include/stdio.h:430: warning: redundant redeclaration of 'sscanf'
/usr/include/stdio.h:476: warning: redundant redeclaration of 'vfscanf'
/usr/include/stdio.h:481: warning: redundant redeclaration of 'vscanf'
/usr/include/stdio.h:484: warning: redundant redeclaration of 'vsscanf'
{standard input}: Assembler messages:
{standard input}:13389: Error: unrecognized opcode `rorw $8,$2'
make[1]: *** [announcer.o] Error 1
make[1]: Leaving directory `/home/allen/buildroot-2009.11/transmission/transmission-2.04/libtransmission'
make: *** [all-recursive] Error 1  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-26 17:20 gouzhuang
@bgs90@126.com
>>Compiler: g++
这个不对呀!应该是mipsel-linux-g++
请按下面的步骤再试一下:
1. export PATH=/opt/cross/gcc442-uc0928/usr/bin:$PATH
2. make distclean
3. CFLAGS='-Os -pipe -I/opt/cross/gcc442-uc0928/usr/include -mtune=mips32 -mabi=32 -msoft-float -static-libgcc -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64' \
PKG_CONFIG='/home/allen/buildroot-2009.11/output/host/usr/bin/pkg-config' \
LDFLAGS='-L/opt/cross/gcc442-uc0928/lib -L/opt/cross/gcc442-uc0928/usr/lib' \
./configure --host=mipsel-linux --prefix=/usr --disable-gtk --disable-nls CPPFLAGS=-DTR_EMBEDDED

如果还不行把configure完整输出发给我:gouzhuang (at) gmail.com 我的MSN也是用这个邮件地址。  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-10-27 15:43 bgs90@126.com
还是不行,这个出不来,>>Compiler: g++
这个不对呀!应该是mipsel-linux-g++
已经发到你邮箱了。  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2010-12-02 10:19 gouzhuang
@with-unknown-capability
>What does the "with unknown capability" bit mean?
I don't know, but it doesn't cause any problem for me. I guess it is a flag set by gcc that the file command doesn't know. You can just ignore it.

>The executables tend not to run fully/without errors on the player, unless I chroot into the buildroot rootfs.
what's the uclibc version on your player? probably different from that of the buildroot. Maybe your program depends on extra libraries which the stock player does not have. What I do is simply replace the player's uclibc with that from buildroot.  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2011-07-23 00:16 peija
博主您好, 我用buildroot2011.05make的时候,最后生成rootfs镜像文件的时候出错, 找了很就都没找到原因,对这个不是很熟悉, 我在menuconfig里 filesystem image里选了 cramfs root filesystem 和 tar root filesystem .最后的出错信息是:
fakeroot: preload library not found, aborting.
make: *** [/home/zgy/buildroot-2011.05/output/images/rootfs.cramfs] 错误 1
希望您在闲暇之余能给我一些帮助,谢谢。  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2011-07-25 08:44 gouzhuang
@peija
你是否安装了cramfsprogs软件包?  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2012-07-15 07:49 jzm
学习编译,谢谢大大的教程,深受启发  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2013-02-05 15:41 qing
我刚make

就报错误了。

package/atk/atk.mk:45: *** recipe commences before first target  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2013-02-05 16:05 qing
@qing

make 退到 2.81

没问题了。。  回复  更多评论
  

# re: Buildroot - 让交叉编译更轻松 2015-11-18 16:26 huying
@gouzhuang
请问如何开启C++编译选项啊?  回复  更多评论
  

只有注册用户登录后才能发表评论。