使用buildroot創建嵌入式系統和交叉編譯工具鏈


Buildroot的簡單使用--使用buildroot創建嵌入式系統和交叉編譯工具鏈

 

Buildroot  是什么

BuildrootLinux平台上一個構建嵌入式Linux系統的框架。它可以讓你從toolchain,boot,kernel到文件系統及各類應用(qt,gstream,busybox)一站式make,省去了你編譯工具和內核版本的不匹配,glibc庫不對啦,每次編譯都要去改arch cross_compileN多煩惱.你也不用在去瞎找各種tarball下載了,它都給你提供了官方的下載途徑.總之就是省心省力.具體可看官網說明.

 

安裝

 

必須安裝的工具包括:

 

which

sed

make (version 3.81 or any later)

binutils  

build-essential (only for Debian based systems)  

gcc (version 4.8 or any later)

g++ (version 4.8 or any later)

bash

patch

gzip

bzip2

perl (version 5.8.7 or any later)

tar

cpio

unzip

rsync

file (must be in /usr/bin/file)

bc

 

wget

 

為了使用它的圖形界面,需要安裝一些額外的庫。我發主要使用menuconfig,需要libncurses5-dev 庫

Ubuntu上:

~$ sudo apt install  git build-essential wget cpio unzip rsync bc libncurses5-dev screen

工具基本上就安裝全了。

簡單使用 -- 創建一個qemu虛擬機

環境:ubuntu20 桌面版+buildroot-2020.02.4.tar.gz

ubuntu16桌面版+ buildroot-2020.02.3.tar.gz,總是出現各種各樣的錯誤。。換成20版本就搞定了。。

 

注意:

一定要把vpn環境配置好。否則make的時候幾乎不能搞不定。

在虛擬機里make的時候,把內存調的大一點,否則經常出現頁錯誤。

 

手冊上建議buildroot的所有操作都是普通用戶操作,不需要root權限。

 

 

config目錄下,有很多預先定義好的配置,就是xxx_defconfig文件。例如qemu_mips32r6_malta_defconfig ,在 buildroot根目錄下

make qemu_mips32r2el_malta_defconfig

就導入了一個適合qume上運行的mips的配置。Once executed, this command will generate the file .config, which contains all the packages, kernel, toolchain and properties needed for our image.

 

make list-defconfigs 可以查看所有的defconfig文件。

 

導入之后,再調用menuconfig界面進行微調。很多時候不需要怎么微調了。

$ make menuconfig

$ make menuconfig 跳出配置界面。注意這個界面的退出是 Press <Esc><Esc> to exit,不然找不到退出的位置。。。。

 

可配置以下的各個選項:

Target options  --->選擇目標板架構特性。
Build options  --->配置編譯選項。
Toolchain  ---> 配置交叉工具鏈,使用buildroot自己編譯制作工具鏈還是使用外部提供。
System configuration  --->
Kernel  --->  配置內核版本
Target packages  --->配置應用
Filesystem images  --->
Bootloaders  --->
Host utilities  --->
Legacy config options  --->

 

下圖是qemu_mips32r6_malta_defconfig這個配置文件中 target options的配置

 

 

關於tool chain,需要注意的是的kernel headers這一項。這里制定編譯toolchain時,用到的linux頭文件的版本。

 

 

網上很多文章都說錯了,不是上位機的uname看到的那一個。。。

 編譯C的時候會去訪問Linux kernel headers(*.h)文件,就是這些頭文件定義了用戶空間的程序與內核之間的交互接口(系統調用,數據結構等),重點是這個庫接口是向后兼容,也就是說你不知道選哪個就選舊版的吧.選太新的是不能識別舊版的內核通信,但選舊版頭文件仍可以和新版內核通信,詳細說明如下:

Change the version of the Linux kernel headers used to build the toolchain. This item deserves a few explanations. In the process of building a cross-compilation toolchain, the C library is being built. This library provides the interface between userspace applications and the Linux kernel. In order to know how to "talk" to the Linux kernel, the C library needs to have access to the Linux kernel headers (i.e. the .h files from the kernel), which define the interface between userspace and the kernel (system calls, data structures, etc.). Since this interface is backward compatible, the version of the Linux kernel headers used to build your toolchain do not need to match exactly the version of the Linux kernel you intend to run on your embedded system. They only need to have a version equal or older to the version of the Linux kernel you intend to run. If you use kernel headers that are more recent than the Linux kernel you run on your embedded system, then the C library might be using interfaces that are not provided by your Linux kernel.  做過交叉編譯工具的都知道,就是要盡量和要編譯的內核的版本一致,或者低一些版本的。

 這里選擇 same as kernel being builtCustom kernel headers series條目就會自動出現並且和當前kernel版本一致。(如果是直接 make menuconfig ,沒有選用某個defconfig,也就沒有same as kernel being built這個了)

 關於C library, uClibc是專為嵌入式簡化的C,小巧精簡,但不兼容glibc,是獨立的實現的. 這個選項也很重要,需要注意目標系統上是哪個c庫。

 

 Once everything is configured, the configuration tool generates a .config file that contains the entire configuration. This file will be read by the top-level Makefile.  

開始make,要等挺長時間的。

 $ make

 Note: Don’t use the -j parameter of make here, buildroot will figure that out on its own instead.

  

Make完成之后,Buildroot output is stored in a single directory, output/. This directory contains several subdirectories:

images/ 生成的內核和根文件系統.

build/ 構建過程中的所有的組件,各種程序,一個文件夾就是一個程序.

host/ contains both the tools built for the host, and the sysroot of the target toolchain. The former is an installation of tools compiled for the host that are needed for the proper execution of Buildroot, including the cross-compilation toolchain. The latter is a hierarchy similar to a root filesystem hierarchy. It contains the headers and libraries of all user-space packages that provide and install libraries used by other packages. However, this directory is not intended to be the root filesystem for the target: it contains a lot of development files, unstripped binaries and libraries that make it far too big for an embedded system. These development files are used to compile libraries and applications for the target that depend on other libraries.

交叉編譯工具鏈默認存放於 output/host/. 把 output/host/bin/添加到 PATH 中就可以方便的使用了。也可以  make sdk 把相關工具打包。

staging/ is a symlink to the target toolchain sysroot inside host/, which exists for backwards compatibility.

target/ which contains almost the complete root filesystem for the target: everything needed is present except the device files in /dev/ (Buildroot can’t create them because Buildroot doesn’t run as root and doesn’t want to run as root). Also, it doesn’t have the correct permissions (e.g. setuid for the busybox binary). Therefore, this directory should not be used on your target. Instead, you should use one of the images built in the images/ directory. If you need an extracted image of the root filesystem for booting over NFS, then use the tarball image generated in images/ and extract it as root. Compared to staging/, target/ contains only the files and libraries needed to run the selected target applications: the development files (headers, etc.) are not present, the binaries are stripped.

  

然后就可以在qemu里啟動linux了。啟動的命令可以在 /board/qemu/mips32r2el-malt/readme.txt文件中找到。這里的命令是:

qemu-system-mipsel -M malta -kernel output/images/vmlinux -serial stdio -drive file=output/images/rootfs.ext2,format=raw -append "rootwait root=/dev/hda" -net nic,model=pcnet -net user

 

虛擬機正常運行:

 

 

 

 

參考:

手冊  https://buildroot.org/downloads/manual/manual.html

 https://blog.conan.io/2019/08/27/Creating-small-Linux-images-with-Buildroot.html

  How to create small VMs with buildroothttp://people.redhat.com/~thuth/blog/general/2019/01/28/buildroot.html

 buildroot使用介紹  https://www.bbsmax.com/A/D854LrA65E/   (推薦)

  https://blog.csdn.net/mxgsgtc/article/details/53047562

 https://www.thirtythreeforty.net/posts/2020/01/mastering-embedded-linux-part-3-buildroot/ 系列文章,推薦

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM