嵌入式Linux 交叉編譯工具鏈

前言

這是前2篇:

.c之類的源碼不能直接運行, 需要編譯鏈接生成可執行文件, 才能執行. 這就需要工具鏈(toolchain). Embedded Systems Programming Hello World for ARM - 2020 這篇講的很好, 摘錄部分如下:

Toolchain consists of a compiler, linker, assembler, and a debugger.
工具鏈是編程工具的集合, 包含編譯, 鏈接器, 彙編器, 調試器.
在這裏插入圖片描述

PC可以直接編寫源代碼, 然後裝個編譯器(如gcc)就能生產出自己可以執行的文件, 這種叫native compilation. 如在x86編寫代碼, 在x86編譯, 在x86運行.

但嵌入式設備不一定行, 限於空間, 算力, 一般無力或者不方便直接編寫/編譯.交叉編譯(cross-compilation)是在一個平臺上生成另一個平臺上的可執行代碼. 可以在主機安裝交叉編譯工具鏈編譯生成可執行文件, 然後再把可執行文件放到嵌入式設備運行. 如在x64架構的Linux機器上編譯ARM平臺, 需要 Linux-based ARM-targeting cross compiler.

Toolchains have a loose name convention like arch [-vendor] [-os] - eabi
arch - refers to target architecture (which in our case is ARM)
vendor - refers to toolchain supplier
os - refers to the target operating system
eabi - refers to Embedded Application Binary Interface

some illustrations as follows :

arm-none-eabi - This tool chain targets for ARM architecture, has no vendor, does not target an operating system and complies with the ARM EABI.
arm-none-linux-gnueabi - This toolchain targets the ARM architecture, has no vendor, creates binaries that run on the Linux operating system, and uses the GNU EABI. It is used to target ARM-based Linux systems.
arm-linux-gcc - This is actually binary for gcc which produces objects for ARM architecture to be run on Linux with default configuration (abi) provided by toolchain.
arm-eabi - Android ARM compiler
i686-apple-darwin10-gcc-4.2.1 - This toolchain targets the Intel i686 architecture, the vendor is Apple, and the target OS is Darwin version 10.

i.mx6ull是ARM的Cortex-A7架構, 可以用Linaro公司產的arm-linux-gnueabihf, 大概是The GNU Embedded Application Binary Interface for armhf architecture的意思, 可參見 各版本arm-gcc區別與安裝或者交叉編譯器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的區別.

安裝arm-linux-gnueabihf

系統是Ubuntu18, 常理來講, 只需要sudo apt-get install gcc-arm-linux-gnueabihf一條命令安裝最新的即可, 但原子的手冊上不推薦最新的7.x, 說是發現編譯後的Uboot無法運行, 推薦安裝4.9版本, 下載地址, 因爲是64位的系統, 需要下載如圖:
在這裏插入圖片描述
之後步驟:

  • 創建目錄: sudo mkdir /usr/local/arm
  • 複製下載的壓縮包到創建的目錄: sudo cp gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz /usr/local/arm
  • 切換到目錄cd /usr/local/arm, 解壓: sudo tar vxf gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf
  • 修改環境變量: sudo vim /etc/profile, 末尾添加 export PATH=$PATH:/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin:
    在這裏插入圖片描述
  • 保存退出, 如原子手冊, 還要安裝庫: sudo apt-get install lsb-core lib32stdc++6
  • 重啓Ubuntu. 然後用arm-linux-gnueabihf-gcc -v查看版本號, 最後應該顯示gcc version 4.9.4 (Linaro GCC 4.9-2017.01)

檢驗

上篇用NFS共享了/home/karoto/linux/nfs目錄, 在這個目錄裏, 編寫main.c文件:

#include <stdio.h>

int main()
{
    printf("hello, world!\r\n");
    return 0;
}

先用gcc: gcc -o main main.c, 運行./main, 輸出hello, world!, 然後在嵌入式平臺運行, 輸出:
在這裏插入圖片描述
運行不了, 用file main辨識一下main的文件類型:
在這裏插入圖片描述
怪不得在32bit A7上不能運行, 需要用上小節的交叉編譯: arm-linux-gnueabihf-gcc -o main1 main.c

file main1查看:
在這裏插入圖片描述
運行./main1, 在服務器的Ubuntu18上出錯, 然後在嵌入式平臺運行, 輸出正常:
在這裏插入圖片描述

微信公衆號

歡迎掃描關注我的微信公衆號, 及時獲取最新文章:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章