Ubuntu18.04 LTS x64 构建ARM交叉编译环境(尝试,但失败了!!!估计是编译器没选对)
【测试而已,由于需要了解编译器和处理器体系,因此先放弃该方法】
动机
入门嵌入式开发,又需要 Windows 又需要 Linux,但资料给的竟然是 Ubuntu9,导致我不能使用 VSCode Remote SSH 来远程开发。
所以决定自己配置ARM交叉编译环境
虚拟机环境配置
Ubuntu18.04 x64 LTS Desktop: 从UTSC Mirror 镜像源下载即可,安装完直接换USTC的软件源,安装openssh-server以开启SSH服务端,并[关闭开机自启的图形界面服务(//www.jianshu.com/p/36dcf5185f01)节省资源。在VMware分配多点CPU和内存。
sudo systemctl set-default graphical.target # 开启用户图形界面
sudo systemctl set-default multi-user.target # 关闭用户图形界面
sudo reboot
crosstool-NG(ct-ng): 作为crosstool的继任者,接替担任交叉编译环境的历史重任
Mobaxterm Home Edition: 是Windows下最好的免费远程工具,还支持串口、rdp等协议
安装 crosstool-NG
mkdir ~/ct-src
tar -Jxvf crosstool-ng-1.24.0.tar.xz -C ${HOME}/ct-src
cd $HOME/ct-src/crosstool-ng-1.24.0/
sudo apt install build-essential gawk flex libncurses-dev texinfo help2man bison byacc libtool libtool-bin curl lzip
./configure --prefix=/opt/ct-ng #这里prefix指的是安装路径
make
sudo make install
export PATH="${PATH}:/opt/ct-ng/bin"
ct-ng -v
sudo nano /etc/profile #写入系统环境变量,即在文件末尾增加export PATH="${PATH}:/opt/ct-ng/bin"
source /etc/profile
Shell completion
crosstool-NG comes with a shell script fragment that defines bash-compatible completion. That shell fragment is currently not installed automatically.
To install the shell script fragment, you have two options:
– install system-wide, most probably by copying ct-ng.comp into /etc/bash_completion.d/, or
– install for a single user, by copying ct-ng.comp into ${HOME}/ and sourcing this file from your ${HOME}/.bashrc.
使用ct-ng配置交叉编译工具链
mkdir ct-out && cd ct-out
mkdir build src x-tools #创建三个目录用于后续使用
cd build
ct-ng list-samples # 列出源码包的samples架构配置示例,也可手动查看 ls ../../crosstool-ng-1.24.0/samples/
ct-ng arm-unknown-linux-gnueabi # 也可手动复制 cp ../../crosstool-ng-1.24.0/samples/arm-unknown-linux-gnueabi/crosstool.config .config 原理是将crosstool-ng源码包中samples/arm-unknown-linux-gnueabi/下的crosstool.config拷贝到交叉工具链的输出目录`build`下
创建好的目录树如下
my@ubuntu:~/ct-out/build$
tree ~/ct-out/
~/ct-out
├── build
│ └── crosstool.config
├── src
└── x-tools
3 directories, 1 file
ct-ng menuconfig # 菜单配置
当然如果你有别人配好的 .config 文件,那么直接跳过.config配置部分,直接ct-ng build就可以了
支持的架构有
my@ubuntu:~/Desktop/work-dir$ ls ../../crosstool-ng-1.24.0/samples/
aarch64-rpi3-linux-gnu
aarch64-unknown-linux-android
aarch64-unknown-linux-gnu
aarch64-unknown-linux-uclibc
alphaev56-unknown-linux-gnu
alphaev67-unknown-linux-gnu
arc-arc700-linux-uclibc
arc-multilib-elf32
arc-multilib-linux-uclibc
arm-bare_newlib_cortex_m3_nommu-eabi
arm-cortex_a15-linux-gnueabihf
arm-cortexa5-linux-uclibcgnueabihf
arm-cortex_a8-linux-gnueabi
arm-cortexa9_neon-linux-gnueabihf
armeb-unknown-eabi
armeb-unknown-linux-gnueabi
armeb-unknown-linux-uclibcgnueabi
arm-multilib-linux-uclibcgnueabi
arm-nano-eabi
arm-unknown-eabi
arm-unknown-linux-gnueabi
arm-unknown-linux-musleabi
arm-unknown-linux-uclibcgnueabi
arm-unknown-linux-uclibcgnueabihf
armv6-nommu-linux-uclibcgnueabi
armv6-rpi-linux-gnueabi
armv7-rpi2-linux-gnueabihf
armv8-rpi3-linux-gnueabihf
avr
i586-geode-linux-uclibc
i686-centos6-linux-gnu
i686-centos7-linux-gnu
i686-nptl-linux-gnu
i686-ubuntu12.04-linux-gnu
i686-ubuntu14.04-linux-gnu
i686-ubuntu16.04-linux-gnu
i686-w64-mingw32
i686-w64-mingw32,nios2-spico-elf
m68k-unknown-elf
m68k-unknown-uclinux-uclibc
mips64el-multilib-linux-uclibc
mips-ar2315-linux-gnu
mipsel-multilib-linux-gnu
mipsel-sde-elf
mipsel-unknown-linux-gnu
mips-malta-linux-gnu
mips-unknown-elf
mips-unknown-linux-uclibc
moxiebox
moxie-unknown-elf
msp430-unknown-elf
nios2-altera-linux-gnu
nios2-unknown-elf
powerpc-405-linux-gnu
powerpc64le-unknown-linux-gnu
powerpc64-multilib-linux-gnu
powerpc64-unknown-linux-gnu
powerpc-8540-linux-gnu
powerpc-860-linux-gnu
powerpc-e300c3-linux-gnu
powerpc-e500v2-linux-gnuspe
powerpc-unknown-linux-gnu
powerpc-unknown-linux-uclibc
powerpc-unknown-linux-uclibc,m68k-unknown-uclinux-uclibc
powerpc-unknown_nofpu-linux-gnu
riscv32-hifive1-elf
riscv32-unknown-elf
riscv64-unknown-elf
riscv64-unknown-linux-gnu
s390-ibm-linux-gnu
s390x-ibm-linux-gnu
samples.mk
sh-multilib-linux-gnu
sh-multilib-linux-uclibc
sh-unknown-elf
sparc64-multilib-linux-gnu
sparc-leon-linux-uclibc
sparc-unknown-linux-gnu
x86_64-centos6-linux-gnu
x86_64-centos7-linux-gnu
x86_64-multilib-linux-gnu
x86_64-multilib-linux-musl
x86_64-multilib-linux-uclibc
x86_64-multilib-linux-uclibc,moxie-unknown-moxiebox
x86_64-multilib-linux-uclibc,powerpc-unknown-elf
x86_64-ubuntu12.04-linux-gnu
x86_64-ubuntu14.04-linux-gnu
x86_64-ubuntu16.04-linux-gnu
x86_64-unknown-linux-gnu
x86_64-unknown-linux-uclibc
x86_64-w64-mingw32
x86_64-w64-mingw32,arm-cortexa9_neon-linux-gnueabihf
x86_64-w64-mingw32,x86_64-pc-linux-gnu
xtensa-fsf-elf
xtensa-fsf-linux-uclibc
my@ubuntu:~/Desktop/work-dir$
根据需要设定源码包路径和交叉编译工具链的安装路径
Paths and misc options —>
- 源码包路径:设置Local tarballs directory为
${HOME}/ct-src/crosstool-ng-1.24.0/
- 交叉编译器的安装路径:设置 Prefix directory,把其中的
${HOME}/x-tools
改为${HOME}/ct-out/x-tools
修改交叉编译工具针对的架构(S3C2440)
Target options—>
Target architecture(arm)
[*] Use the MMU
Endianness(Little endian)
Bitness(32-bit)
(arm920t) Emit assembly for CPU
Floating point: (software (no FPU))
() Use specific FPU
其它保持默认值,退出并保存配置工具
以上配置也可通过直接修改.config文件来完成
当然也可以配置其他东西,如 Toolchain options
参考:crosstool-ng参数配置_gmf
剩下的 C-library & C compiler & Debug facilities & Companion libraries & Companion tools
用于配置生成的交叉工具链所用的gcc等版本,这里我暂时不懂所以起先不管它
执行ct-ng build编译工具链
ct-ng build
编译要几个小时
因为编译过程会自动下载源码
编译日志见build.log文件
若依赖包wget自动下载失败,也可手动下载放到.build/tarballs/目录
编程完成后,找到编译生成的文件路径,测试
export "PATH=$PATH:$HOME/ct-out/x-tools/arm-s3c2440-linux-gnueabi/bin"
arm-s3c2440-linux-gnueabihf-gcc -v # 测试
测试结果
my@ubuntu:~/ct-out/build$ arm-s3c2440-linux-gnueabi-gcc -v
Using built-in specs.
COLLECT_GCC=arm-s3c2440-linux-gnueabi-gcc
COLLECT_LTO_WRAPPER=/home/my/ct-out/x-tools/arm-s3c2440-linux-gnueabi/libexec/gcc/arm-s3c2440-linux-gnueabi/8.3.0/lto-wrapper
Target: arm-s3c2440-linux-gnueabi
Configured with: /home/my/ct-out/build/.build/arm-s3c2440-linux-gnueabi/src/gcc/configure --build=x86_64-build_pc-linux-gnu --host=x86_64-build_pc-linux-gnu --target=arm-s3c2440-linux-gnueabi --prefix=/home/my/ct-out/x-tools/arm-s3c2440-linux-gnueabi --with-sysroot=/home/my/ct-out/x-tools/arm-s3c2440-linux-gnueabi/arm-s3c2440-linux-gnueabi/sysroot --enable-languages=c --with-cpu=arm920t --with-float=soft --with-pkgversion='crosstool-NG 1.24.0 - yucloud20200928' --with-bugurl=//yucloud.cnblogs.com/p/arm_crosscompile.html --enable-__cxa_atexit --disable-libmudflap --disable-libgomp --disable-libssp --disable-libquadmath --disable-libquadmath-support --disable-libsanitizer --disable-libmpx --with-gmp=/home/my/ct-out/build/.build/arm-s3c2440-linux-gnueabi/buildtools --with-mpfr=/home/my/ct-out/build/.build/arm-s3c2440-linux-gnueabi/buildtools --with-mpc=/home/my/ct-out/build/.build/arm-s3c2440-linux-gnueabi/buildtools --with-isl=/home/my/ct-out/build/.build/arm-s3c2440-linux-gnueabi/buildtools --enable-lto --with-host-libstdcxx='-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm' --enable-threads=posix --enable-target-optspace --disable-plugin --disable-nls --disable-multilib --with-local-prefix=/home/my/ct-out/x-tools/arm-s3c2440-linux-gnueabi/arm-s3c2440-linux-gnueabi/sysroot --enable-long-long
Thread model: posix
gcc version 8.3.0 (crosstool-NG 1.24.0 - yucloud20200928)
成功后再使用软链接把所有可执行文件通用化
sudo nano link.sh
sudo chmod +x link.sh
sudo chmod +x *