linux学习——交叉编译器配置流程
本文为交叉编译器的配置过程以及相关知识的简单介绍。
一、预备知识
1、交叉编译与交叉编译器。
交叉编译:简单来说就是一个平台上生成另一个平台能够执行的可执行代码。
例如:我们平时使用的个人电脑的CPU是x86架构的指令集,所以编译生成的可执行代码只能够在x86架构的平台上运行,如果要在ARM的平台上运行,需要由ARM的指令体系编译生成能够在ARM平台能够执行的代码。我们在PC(x86指令集cpu)上编译能够在ARM开发板上执行的可执行代码,这就是交叉编译。
交叉编译器:在一种计算机环境中能编译出在另外一种环境下运行的代码的编译程序。
2、uclibc与 glibc。
glibc:glibc = GNU C library,是GNU项目所实现的标准库,支持多种平台。但是臃肿而庞大。
3.buildroot:Buildroot
buildroot:Buildroot是一个嵌入式开发领域内的成套的嵌入式开发环境。可以用来制作自己的交叉编译器,自己的根文件系统,把 uboot,linux kernel集成到Buildroot一起编译。
4.crosstool -ng:
crosstools -ng是个交叉编译器的制作工具。
二、交叉编译器制作过程
建议配置交叉编译器不要使用 root 账号,并且配置在 \opt 目录下面。
1.准备源码包及安装路径:
(我们从 https://buildroot.org/ 网站去下载需要用到的 buildroot 包)。
从这里选择自己需要的 buildroot 包
我们最好将其配置在/opt下,并新建一文件夹例如/opt/dl
[lwn@VM_255_164_centos ]$ cd /opt/
[lwn@VM_255_164_centos opt]$ mkdir dl
[lwn@localhost src3]$ wget https://buildroot.org/downloads/buildroot-2009.11-rc1.tar.bz2
这个时候我们可以看到已经下载成功,然后对其解压缩(如果是 file.tar.gz 的压缩包使用 tar -zcvf file.tar.gz 解压缩)
[lwn@VM_255_164_centos dl]$ ls
buildroot-2012.08.tar.bz2
[lwn@VM_255_164_centos dl]$ sudo tar -xjf buildroot-2012.08.tar.bz2
[lwn@VM_255_164_centos dl]$ ls
buildroot-2012.08 buildroot-2012.08.tar.bz2
2.配置安装 buildroot
[lwn@VM_255_164_centos dl]$ cd buildroot-2012.08/
[lwn@VM_255_164_centos buildroot-2012.08]$ ls
CHANGES Config.in board configs fs package target
COPYING Makefile boot docs linux support toolchain
[lwn@VM_255_164_centos buildroot-2012.08]$ export TERM=vt100
[lwn@VM_255_164_centos buildroot-2012.08]$ make menuconfig
/opt/dl/buildroot-2012.08/.config - Buildroot 2012.08 Configuration
──────────────────────────────────────────────────────────────────────────────
┌──────────────────── Buildroot 2012.08 Configuration ────────────────────┐
│ Arrow keys navigate the menu. <Enter> selects submenus --->. │
│ Highlighted letters are hotkeys. Pressing <Y> selectes a feature, │
│ while <N> will exclude a feature. Press <Esc><Esc> to exit, <?> for │
│ Help, </> for Search. Legend: [*] feature is selected [ ] feature is │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Target Architecture (ARM (little endian)) ---> │ │
│ │ Target Architecture Variant (arm920t) ---> │ │
│ │ Target ABI (EABI) ---> │ │
│ │ Build options ---> │ │
│ │ Toolchain ---> │ │
│ │ System configuration ---> │ │
│ │ Package Selection for the target ---> │ │
│ │ Host utilities ---> │ │
│ │ Filesystem images ---> │ │
│ │ Bootloaders ---> │ │
│ │ Kernel ---> │ │
│ └────v(+)─────────────────────────────────────────────────────────────┘ │
├─────────────────────────────────────────────────────────────────────────┤
│ <Select> < Exit > < Help > │
buildroot-2012.08 make menuconfig 选项配置说明
Target Architecture (ARM (little endian)) ---> //选择 ARM(little endian)
Target Architecture Variant (arm920t) ---> //根据自己的CPU来选择,我们选择 arm920t
Target ABI (EABI) ---> //选择 EABI
Build options ---> //参照以下设置修改
($(TOPDIR)/dl) Download dir //设置下载目录为$(TOPDIR/dl)
($(TOPDIR)/arm920t) Host dir //设置 为$(DOPDIR/arm920t)
[*] prefer static libraries //选上 prefer static libraries
其它选项都不要选,如果是要设置值的地方就使用默认值
Toolchain ---> //参照以下设置修改
Kernel Headers (Linux 2.6 (manually specified version)) --->
(2.6.37) linux version 选定内核版本为2.6.37
... ...
[*] Enable compiler tls support
[*] Enable MMU support
[*] Use software floating point by default
[*] Enable large file (files > 2 GB) support
[*] Enable IPv6 support
[*] Enable RPC support
[*] Enable WCHAR support
[*] Enable C++ support
其它选项都不要选,如果是要设置值的地方就使用默认值
System configuration --->
Init system (None) ---> 把Busbyox编译去掉
这里面所有的都不要选,如果是要设置值的地方就使用默认值
Package Selection for the target --->
这里面所有的选项都不要选,除了:
Libraries --->
Compression and decompression --->
[*] lzo
-*- zlib
Host utilities --->
这里面所有的选项都不要选
Filesystem images --->
这里面所有的选项都不要选
Bootloaders --->
这里面所有的选项都不要选
Kernel ---
这里面所有的选项都不要选
//以上所有配置完成之后最后要保存生成.config文件。
──────────────────── Buildroot 2012.08 Configuration ────────────────────┐
│ Arrow keys navigate the menu. <Enter> selects submenus --->. │
│ Highlighted letters are hotkeys. Pressing <Y> selectes a feature, │
│ while <N> will exclude a feature. Press <Esc><Esc> to exit, <?> for │
│ Help, </> for Search. Legend: [*] feature is selected [ ] feature is │
│ ┌────^(-)─────────────────────────────────────────────────────────────┐ │
│ │ Build options ---> │ │
│ │ Toolchain ---> │ │
│ │ System configuration ---> │ │
│ │ Package Selection for the target ---> │ │
│ │ Host utilities ---> │ │
│ │ Filesystem images ---> │ │
│ │ Bootloaders ---> │ │
│ │ Kernel ---> │ │
│ │ --- │ │
│ │ Load an Alternate Configuration File │ │
│ │ Save an Alternate Configuration File │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
├─────────────────────────────────────────────────────────────────────────┤
│ <Select> < Exit > < Help >
/opt/dl/buildroot-2012.08/.config - Buildroot 2012.08 Configuration
──────────────────────────────────────────────────────────────────────────────
┌─────────────────────────────────────────────────────┐
│ Enter a filename to which this configuration │
│ should be saved as an alternate. Leave blank to │
│ abort. │
│ ┌─────────────────────────────────────────────────┐ │
│ │/opt/dl/buildroot-2012.08/.config │ │
│ └─────────────────────────────────────────────────┘ │
│ │
├─────────────────────────────────────────────────────┤
│ < Ok > < Help > │
└─────────────────────────────────────────────────────┘
//最后在编译(make)
[lwn@localhost buildroot-2012.08]$ make
接下来就是漫长的等待。。。(我在本地虚拟机等待编译完成花了一个多小时)
3.检查是否配置成功
如果编译成功,会显示以下信息,并且在 /opt/dl/buildroot-2012.08/ARM920t/usr/bin/ 目录下会生成许多编译器
编译完成成功最后结果说明:
=======================
... ... ...
touch /apps/buildroot-2012.08/output/target/etc/ld.so.conf
mkdir -p /apps/buildroot-2012.08/output/target/var/cache/ldconfig
if [ -x "/apps/buildroot-2012.08/arm920t/usr/bin/arm-unknown-linux-uclibcgnueabi-ldconfig" ]; \
then \
/apps/buildroot-2012.08/arm920t/usr/bin/arm-unknown-linux-uclibcgnueabi-ldconfig -r /apps/buildroot-2012.08/output/target; \
else \
/sbin/ldconfig -r /apps/buildroot-2012.08/output/target; \
fi
/apps/buildroot-2012.08/arm920t/usr/bin/arm-unknown-linux-uclibcgnueabi-ldconfig: /usr/lib/libstdc++.so.6.0.14-gdb.py is not a shared library, skipping
/apps/buildroot-2012.08/arm920t/usr/bin/arm-unknown-linux-uclibcgnueabi-ldconfig: can't resolve /usr/X11R6/lib in chroot .: No such file or directory
/apps/buildroot-2012.08/arm920t/usr/bin/arm-unknown-linux-uclibcgnueabi-ldconfig: skipping /usr/X11R6/lib: No such file or directory
( \
echo "NAME=Buildroot"; \
echo "VERSION=2012.08"; \
echo "ID=buildroot"; \
echo "VERSION_ID=2012.08"; \
echo "PRETTY_NAME=\"Buildroot 2012.08\"" \
) > /apps/buildroot-2012.08/output/target/etc/os-release
[lwn@localhost bin]$ ls /opt/dl/buildroot-2012.08/ARM920t/usr/bin/
aclocal
aclocal-1.11
arm-linux-addr2line
arm-linux-ar
arm-linux-as
arm-linux-c++
arm-linux-cc
arm-linux-c++filt
arm-linux-cpp
arm-linux-elfedit
arm-linux-g++
arm-linux-gcc
arm-linux-gcc-4.5.4
arm-linux-gccbug
arm-linux-gcov
arm-linux-gprof
arm-linux-ld
arm-linux-ld.bfd
arm-linux-ldconfig
arm-linux-ldd
arm-linux-nm
arm-linux-objcopy
arm-linux-objdump
arm-linux-ranlib
arm-linux-readelf
arm-linux-size
arm-linux-strings
arm-linux-strip
4.确认可用。
最后我们再用hello.c文件测试一下是否可以成功编译。我们用自己做的交叉编译器对hello.c进行编译,然后用 file 查看生成的a.out文件属性可以看到
其是在ARM上运行的,用tftp将其下载到ARM开发板上就能运行了。
[lwn@localhost src3]$ /opt/dl/buildroot-2012.08/ARM920t/usr/bin/arm-linux-gcc hello.c
[lwn@localhost src3]$ ls
a.out hello.c
[lwn@localhost src3]$ file a.out
a.out: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
Tips :strip 是一个命令,就是负责剔除符号表信息,这样执行程序的尺寸会变小但是不便调试stripped 表示执行程序中剔除了符号表信息not stripped 表示没有剔除一般最终的程序都会使用strip来减小可执行文件的体积。而调试中的程序则不使用strip。
本文详细介绍如何使用Buildroot配置ARM交叉编译器的过程,包括准备源码包、配置安装Buildroot、验证编译器是否配置成功等步骤。
7108

被折叠的 条评论
为什么被折叠?



