Skip to content

Instantly share code, notes, and snippets.

@AndySze
Last active January 26, 2024 14:03
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndySze/5434903 to your computer and use it in GitHub Desktop.
Save AndySze/5434903 to your computer and use it in GitHub Desktop.
编译树莓派raspberry pi交叉编译工具

树莓派是基于arm内核的小电脑,性能较低,默认cpu频率为700Mhz,所以很多工作并不适合在它上面完成,比如编译软件。所以在它上面的软件需要交叉编译工具来完成。全球范围内,需要交叉编译的芯片非常多,估计超过几万种,每种芯片的编译工具又不仅相同,所以今天我来介绍交叉编译工具的编译工具(是的,很嗷口):crosstool-ng(http://crosstool-ng.org/)。它是为创建交叉编译工具而生。可以为树莓派生成可用的交叉编译工具。当然你可以去raspberry pi 官方提供的工具(http://github.com/raspberrypi/tools.git)。

1、下载ct-ng源码

到ct-ng官网(http://crosstool-ng.org/),下载源码到相应目录。

2、解压源码

tar -xjvf crosstool-ng-1.18.0.tar.bz2 

版本可能不同,请注意

3、安装必要的依赖包

sudo apt-get install bison flex texinfo gawk automake subversion

4、配置

andy@andy-book:~/crossCompile$ ls
crosstool-ng-1.18.0  crosstool-ng-1.18.0.tar.bz2
andy@andy-book:~/crossCompile$ cd crosstool-ng-1.18.0/
andy@andy-book:~/crossCompile/crosstool-ng-1.18.0$ ./configure --prefix=/opt/crosstool-ng-1.18.0

其中的--prefix选项是为了避免不必要的冲突。如果配置过程种出现因为缺少某个依赖包而中断,使用apt-get安装即可,如果找不到相应包,可以google一下。

5、编译及安装

make
make install

如果没有出现错误,应该就编译并安装成功了。为了方便可以把安装位置添加到环境路径中:

@export PATH=$PATH:/opt/crosstool-ng-1.18.0

6、配置crosstool-NG

以上步骤已经把crosstool-NG安装成功。接下来就是用这个工具来构建自己的交叉编译环境了。 找一个合适的目录,运行下面的命令:

ct-ng menuconfig

配置内容比较多,这里之列出必要的选项。没有提到的选项可以默认。

  • Paths & Misc: *

勾选 "Try features marked as EXPERIMENTAL"

设置 "Prefix directory" 为你想最终交错编译工具所在的位置,我只就采用的默认位置,也就所${HOME}/x-tools/目录下

设置 "Number of parallel jobs" 为cpu核心数的1.5倍,另有说法是+1,比如两核心可以设置为3,可以加快编译速度

  • Target options: *

设置 "Target architecture" 为 "ARM"

设置 "Endianness" 为 "Little endian"

设置 "Bitness" 为 "32-bit"

设置 "Architecture level" 为 "armv6zk"

设置 "Emit assembly for CPU" 为 "arm1176jzf-s"

设置 "Tune for CPU" 为 "arm1176jzf-s"

设置 "Use specific FPU" 为 "vfp"

设置 "Floating point" 为 "hardware (FPU)"

设置 "Default instruction set mode" 为 "arm"

勾选 "Use EABI"

  • General toolchain: *

设置 "Tuple's vendor string" 为 "rpi" - 或者自己指定为其它表示,这个字符将显示在工具的名称上。

  • Operating system: *

设置 "Target OS" 为 "linux"

设置 "Linux kernel version" 为相应的linux内核版本官方版本所3.6

Binary utilities:

设置 "Binary format" 为 "ELF"

设置 "binutils version" 我选择的默认版本。

  • C compiler: *

勾选 "Show linaro versions"

设置 "gcc version" 我选的默认版本

勾选 "C++"

设置 "gcc extra config" 为 "--with-float=hard"

勾选 "Link libstdc++ statically into gcc binary"

  • C-library: *

设置 "C library" 为 "eglibc"

设置 "eglibc version" 这个版本可以根据情况选择,我选择的最新版本,没有问题。

7、编译:

执行命令:

ct-ng build

首先会先下载相应的软件包,我尝试过多次才成功,甚至最好是通过vpn翻墙后进行下载,保险一些,因为下载过程中没有提示。只有一个翻装的字符(-|/组成),滚动速度越快说明进展越快,如果长时间没有动,最好重试一下。我试了多次才成功。下载完成后,会自动编译。结束后交叉编译所有的工具即安装成功。 最终它安装在你刚才配置的位置,如${HOME}/x-tools/可以将这个位置添加到系统路径中。 尝试运行gcc应该可以查看到版本信息:

$arm-rpi-linux-gnueabi-gcc -v

gcc version 4.7.3 20121001 (prerelease) (crosstool-NG 1.17.0)
andy@andy-book:~/raspberryPi$ sudo scp helloword  pi@192.168.2.105:/home/pi
[sudo] password for andy: 
The authenticity of host '192.168.2.105 (192.168.2.105)' can't be established.
ECDSA key fingerprint is 52:04:20:5a:87:8e:c2:58:13:5c:d1:4b:fd:63:a7:db.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.105' (ECDSA) to the list of known hosts.
pi@192.168.2.105's password: 
helloword                                     100% 5852     5.7KB/s   00:00 
pi@raspberrypi ~ $ ls
apt-backup  Desktop  helloword  ocr_pi.png  python_games
pi@raspberrypi ~ $ ./helloword 
Hello world!pi@raspberrypi ~ $ ls
apt-backup  Desktop  hello  helloword  ocr_pi.png  python_games
pi@raspberrypi ~ $ ./hello
Hello World++ !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment