Build a GCC cross-compilation toolchain under Ubuntu

1. Introduction to cross-compilation tool chain

1.1 Cross Compilation

Local compilation: Under the current target platform, the compiled program can be run on the current platform

Cross-compilation: Compile on one platform, compile the program, and run it on another platform (compilation and running environment are different, they are cross-compiled)

Cross-compilation is a concept corresponding to local compilation. It is mainly related to embedded development. Since various resources in the embedded system are relatively limited, only enough for the embedded system to run, and there are not many remaining resources, it is difficult to perform direct local compilation.

1.2 Cross Compilation Toolchain

The compilation process is a complex process composed of different sub-functions in sequence, as shown in the figure below:

insert image description here

The compilation process includes functions such as preprocessing, compiling, assembling, and linking. Each sub-function is implemented by a separate tool, and together they form a complete tool set. At the same time, the compilation process is a sequential process, which involves the use order of the tools. Each tool is connected in series according to the sequence relationship, forming aCompile toolchain

Cross Compilation ToolchainIt is a complete set of tools composed of multiple sub-tools for compiling program codes of cross-platform architecture . At the same time, it hides the details of preprocessing, compilation, assembly, linking, etc. When we specify the source file (.c), it will automatically call different subtools according to the compilation process, and automatically generate the final binary program image (.bin ).

Note: Strictly speaking, a cross-compiler just refers to the cross-compiled gcc, but in fact, for convenience, the cross-compiler we often refer to is the cross-compilation toolchain

1.3 Cross-compilation toolchain naming rules

When using a cross-compilation chain, you often see names like the following:

arm-linux-gnueabihf-
arm-none-linux-gnueabi-
arm-cortex_a8-linux-gnueabi-
mips-malta-linux-gnu-

The naming of these cross-compilation chains usually follows certain rules: arch-vender-os-abi, each field is described as follows:

  • arch: target cpu architecture, such as mips, arm, x86, riscv, etc., this field is usually not omitted
  • vendor: Provide the vendor name or vendor-specific information of this compilation toolchain. This field is just identification information and has no practical significance. It can be none, cross, unknown or omitted directly
  • os: the operating system running on the target device, common ones include linux, none (bare metal), etc.
  • abi: Application Binary Interface (Application Binary Interface), the specification of the library function and target image selected by the cross-compilation chain, the common values ​​of this field are abi, eabi (embedded abi), gun (glibc+oabi), gnueabi (glibc +eabi), gnueabihf (hf means the default compilation parameters support hardware floating-point function), etc.

2. Build a GCC cross-compilation toolchain under Ubuntu

The gcc compiler that comes with Ubuntu is for the X86 architecture, but what we want to compile is the code of the ARM architecture, so we need a cross-compiler that runs on the X86 architecture PC and can compile the ARM architecture code.

2.1 Download the cross compiler

This article uses the cross compiler produced by Linaro, Linaro is a non-profit open source software engineering company, the most famous is the Linaro GCC compilation tool chain (compiler), its official website can download the source code
insert image description here

The Linaro website provides a variety of GCC cross-compilation toolchains. Because we are using a Cortex-A7 core development board, we choose arm-linux-gnueabihf, and then download different versions of the compiler according to the 32/64-bit system. Here we choose Download the x86_64 version
insert image description here

2.2 Install the cross compiler

Create directory in Ubuntu: /usr/local/arm

sudo mkdir /usr/local/arm

Copy the downloaded cross compiler to this directory

sudo cp gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz /usr/local/arm/ -f

After the copy is complete, decompress the cross-compilation tool in this directory

sudo tar -vxf gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf.tar.xz

Open the /etc/profile file, modify the environment variables, and add the following content at the end of the file

export PATH=$PATH:/usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin

insert image description here

To use a cross compiler, you also need to install the following libraries

sudo apt-get install lsb-core lib32stdc++6

2.3 Cross Compiler Verification

Use the following command to view the version number of the cross-compilation tool, if the installation is correct, the version number will be displayed

arm-linux-gnueabihf-gcc -v

insert image description here
It can be seen from the figure above that the current version number of the cross compiler is 4.9.4, indicating that the cross compilation tool chain is successfully installed. The command to use the cross compiler is generallyarm-linux-gnueabihf-gcc

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/125020140