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

乐在其中

分享学习Linux的乐趣

  IT博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  23 随笔 :: 0 文章 :: 401 评论 :: 0 Trackbacks
前面介绍了用Buildroot来制作交叉编译工具链,本文将介绍如何用播放机进行本机编译(Native Compiling)。所谓本机编译,是相对于交叉编译(Cross Compiling)而言的。其实这是最常见的编译方式,即在本机编译程序用于在本机或与本机兼容的硬件平台上运行。

一般嵌入式系统的CPU计算性能较差,内存也不多,本机编译要比在PC上交叉编译花更长的时间。但是并非所有软件都支持交叉编译,如mldonkey,这时就只能用本机编译了。

首先要制作本机编译工具链。我们知道用Buildroot可以生成包含本机编译工具的根文件系统,只须在配置Buildroot时请选择下面两个选项:
  1. development files in target filesystem
  2. native toolchain in the target filesystem
make完成后生成的根文件系统在output/target目录下,将它打包:
$ cd output/target
$ tar jcf 
/tmp/mipsel-native-toolchain-gcc442-uc0928-rootfs.tar.bz2 .

这是我制作的一个根文件系统:mipsel-native-toolchain-gcc442-uc0928-rootfs.tar.bz2 (如果不能下载请尝试此链接)

然后在播放机上进入以此文件系统为根的chroot环境,就得到一个本机编译环境。具体步骤如下:
1. 准备一个ext3文件系统的移动硬盘。不能用ntfs或fat,因为它们不支持符号链接(symoblic link)。

2. 将制作好的根文件系统解压到移动硬盘上的toolchain-root目录下(此步骤可在PC或播放机上完成)
# cd <移动硬盘挂载点>
# mkdir toolchain-root
# cd toolchain-root
# tar jxf 
/path/to/mipsel-native-toolchain-gcc442-uc0928-rootfs.tar.bz2

以下步骤在播放机上完成。假设移动硬盘的挂载点是/tmp/usbmounts/sda1
3. 为toolchain-root绑定或挂载/proc,/sys,/dev,/tmp等伪文件系统。这将为给chroot提供更加逼真的根文件系统,使得大部分命令都能正常执行。
# mount --bind /proc /tmp/usbmounts/sda1/toolchain-root/proc
# mount 
--bind /sys /tmp/usbmounts/sda1/toolchain-root/sys
# mount 
--bind /dev /tmp/usbmounts/sda1/toolchain-root/dev
# mount 
-t tmpfs tmpfs /tmp/usbmounts/sda1/toolchain-root/tmp

4. 进入chroot环境并执行shell
# chroot /tmp/usbmounts/sda1/toolchain-root /bin/sh


BusyBox v1.15.3 (2010-04-08 22:24:27 CST) built-in shell (ash)
Enter 'help' for a list of built-in commands.

#

至此我们就得到一个本机编译环境,此时的根目录就是原移动硬盘根录下的toolchain-root目录,在此环境下的所有文件系统访问都将被局限于此目录下。
# pwd
/
# ls 
-l
drwxr
-xr-x    2 root     root         4096 Apr  8 15:12 bin
drwxr
-xr-x   11 root     root         2840 Oct  3 06:36 dev
drwxr
-xr-x    6 root     root         4096 Sep 28 15:46 etc
drwxr
-xr-x    2 root     root         4096 Apr  8 13:51 home
drwxr
-xr-x    2 root     root         4096 Apr  8 15:12 lib
lrwxrwxrwx    
1 root     root           11 Sep 28 15:09 linuxrc -> bin/busybox
drwxr
-xr-x    2 root     root         4096 Apr  8 13:51 mnt
drwxr
-xr-x    2 root     root         4096 Apr  8 13:51 opt
dr
-xr-xr-x   45 root     root            0 Oct  3 06:36 proc
drwxr
-xr-x    2 root     root         4096 Oct  3 06:38 root
drwxr
-xr-x    2 root     root         4096 Apr  8 15:12 sbin
drwxr
-xr-x   10 root     root            0 Oct  3 06:36 sys
drwxrwxrwt    
2 root     root           40 Oct  3 06:37 tmp
drwxr
-xr-x    9 root     root         4096 Apr  8 15:11 usr
drwxr
-xr-x    3 root     root         4096 Dec  1  2009 var
# which gcc
/usr/bin/gcc
# gcc --version
gcc (GCC) 4.4.2
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#

建议在toolchain-root下建一个build目录用作编译工作目录。把要编译的软件源码放到toolchain-root/build目录下,在chroot环境下就在/build目录下。

如果你忘记了自己现在是不是在chroot环境下,有一个很简单的方法可以区分:如果存在/tmp/usbmounts/sda1/toolchain-root目录则不是,否则就是。toolchain-root就好比《盗梦空间》里的哪个陀螺。

注意事项:
1.在播放机上进行编译前最好先执行stopall停止DvdPlayer以释放其占用的内存。
2.建议在硬盘上建一个交换文件以弥补内存的不足。

参考:
1.交叉编译(Cross Compiling)
2.Buildroot - 让交叉编译更轻松
posted on 2010-10-03 15:25 gouzhuang 阅读(5888) 评论(13)  编辑 收藏 引用 所属分类: 嵌入式Linux

评论

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-03 19:50 易吧软件
沙发  回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-04 00:42 ccbcfan
谢谢分享!!  回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-04 22:53 fourcute
是否能传一个国内的空间,google空间下载不了,谢谢您  回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-05 08:41 usli
# chroot toolchain-boot /bin/sh 出错了:
chroot: cannot execute /bin/sh: No such file or directory
怎么回事?多谢.  回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-05 16:42 易吧软件
@usli
使用完整路径,如:
chroot /tmp/usbmounts/sda1/toolchain-boot /bin/sh   回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-05 20:20 gouzhuang
@易吧软件
多谢指正,我确实漏写了/tmp/usbmounts/sda1。已经在博文中修正。  回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-05 22:38 gouzhuang
@fourcute
请试一下这个:
http://cid-33323d948b0eaf12.office.live.com/self.aspx/.Public/mipsel-native-toolchain-gcc442-uc0928-rootfs.tar.bz2  回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-05 22:52 usli
@gouzhuang

~ # chroot /tmp/usbmounts/hdd/toolchain-boot/ /bin/sh
chroot: cannot execute /bin/sh: No such file or directory

还是不行啊,硬盘是播放机格的fat32格式,是不是这个问题?  回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-06 09:18 gouzhuang
@usli
fat32肯定不行。这个文件系统有不少符号连接,请用ext3文件系统。  回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-06 15:12 usli
@gouzhuang

多谢指点。我在另外一个帖子里问了连接打印机的问题,可以给个思路吗,谢谢。  回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-12 08:35 fourcute
@gouzhuang
感谢老大,已经下载,正在搭建中  回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-19 22:27 ccbcfan
@gouzhuang
本机编译环境搭建完成后,我在试着编译一个小程序时出错了,
目标程序:http://rtmpdump.mplayerhq.hu/download/rtmpdump-2.3.tgz
编译过程:
/build/rtmpdump # make SYS=posix
make[1]: Entering directory `/build/rtmpdump/librtmp'
gcc -Wall -DRTMPDUMP_VERSION=\"v2.3\" -DUSE_OPENSSL -O2 -fPIC -c -o rtmp.o rtmp.c
gcc -Wall -DRTMPDUMP_VERSION=\"v2.3\" -DUSE_OPENSSL -O2 -fPIC -c -o log.o log.c
gcc -Wall -DRTMPDUMP_VERSION=\"v2.3\" -DUSE_OPENSSL -O2 -fPIC -c -o amf.o amf.c
gcc -Wall -DRTMPDUMP_VERSION=\"v2.3\" -DUSE_OPENSSL -O2 -fPIC -c -o hashswf.o hashswf.c
gcc -Wall -DRTMPDUMP_VERSION=\"v2.3\" -DUSE_OPENSSL -O2 -fPIC -c -o parseurl.o parseurl.c
ar rs librtmp.a rtmp.o log.o amf.o hashswf.o parseurl.o
ar: creating librtmp.a
gcc -shared -Wl,-soname,librtmp.so.0 -o librtmp.so.0 rtmp.o log.o amf.o hashswf.o parseurl.o -lssl -lcrypto -lz
/usr/bin/ld: /usr/lib/gcc/mipsel-linux-uclibc/4.4.2/../../../libssl.a(s23_meth.o): relocation R_MIPS_HI16 against `a local symbol' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/mipsel-linux-uclibc/4.4.2/../../../libssl.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
make[1]: *** [librtmp.so.0] Error 1
make[1]: Leaving directory `/build/rtmpdump/librtmp'
make: *** [librtmp/librtmp.a] Error 2
/build/rtmpdump #

能指点一下吗?  回复  更多评论
  

# re: 搭建本机编译环境(Building the Native Compiling Environment) 2010-10-20 16:07 gouzhuang
@ccbcfan
我制作的本机编译环境把openssl编成静态库,它不能被链接到动态库librtmp.so。有两个方法解决:
1. 重新把openssl编译成动态库并安装到编译环境中。
2. 把librtmp也编译成静态的:
make LIBS_posix=-ldl SHARED=

第二种方法比较简单,但是编译出的执行文件比较大。  回复  更多评论
  

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