Raspberry Pi 12: The concept of cross compilation, cross compilation tool chain installation, project operation

The concept of cross-compilation, tool chain installation, project operation

1. The concept of cross compilation

What is cross compilation?

Compile:Is to generate executable code on that platform on a platform
Cross compilation: Is to generate executable code on one platform on another platform.
(We write C51 code on windows and compile it into executable code, xx.hex, but run on c51, not on windows)
(We need to write Raspberry Pi code on linux and compile it into executable code, Such as: a.out, but running on Raspberry Pi, not running on Linux)
C51, STM32: Cross compilation occurs on keil (integrated environment)

Since we already have a host compiler, why cross-compile it?

Sometimes it’s because the target platform does not allow or cannot install the compiler we need,
such as: C51
1. Because the target platform is poor in resources, we cannot run the compiler we need
2. Because the target platform has not been established, even operation There is no system, there is no compiler to run. The operating system is also code and must be compiled.

Since it can be cross-compiled, why do we need the host to compile?

Cross compilation is a last resort! Compared with host compilation, cross compilation is subject to more restrictions. Although in theory we can do any form of cross compilation, in fact, due to patent, copyright, and technical restrictions, cross compilation is not always possible. Especially in amateur conditions! For example, we have not been able to generate executable files in Hewlett-Packard’s proprietary som format, so we simply cannot do cross-compilation for the target platform HPPA-HPUX.

The platform needs at least two things to run:

bootloader (boot code) and operating system core

Host : A platform for editing and compiling programs, usually an X86-based PC, usually also called a host.

Target : The system developed by the user is usually a non-X86 platform. The executable code compiled by the host runs on the target.

What tools are needed for cross-compilation?

Cross compiler (cross compiler tool chain)

2. Installation of cross-compilation tool chain for Raspberry Pi (or other platforms)

Installation of cross compilation tool chain

Example: When using keil to compile, you need to select the cross compiler (cross compiler tool chain), C51, STM32;

High code portability: it means that a piece of code can be run on different platforms after being compiled; for example: C51, STM32, the code modification part is not many

We need to use the cross tool chain of the Raspberry Pi:

Download URL:
Raspberry Pi cross-compilation tool chain
tools----code

Pull the compressed package under Windows to Linux and move it to the builtpi folder
//You can also copy from the shared folder to the working directory and
perform the following operations in the terminal:

cd pi       //进入文件夹,
ls          //显示 tools-master.zip
unzip tools-master  //解压文件夹
。。。      //等待
cd tools-master
ls   //显示文件夹
cd arm-bcm2708  
ls
cd gcc-linaro-arm-linux-gnueabihf-raspbian//选择32位编辑器
ls
cd bin
ls   //可以看到一堆绿色的可执行文件
./arm-linux-gnueabihf-gcc -v //去执行该文件
//---------------过程太过繁琐
//1.临时有效:配置环境变量
//PATH 环境变量:目的是省去敲写前面的乱七八糟的文件夹
echo $PATH  //接下来配置环境变量
pwd  //显示当前绝对路径
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/dazai/pi/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin
  //配置环境变量
 cd      //回到根目录
 arm-linux-gnueabihf-gcc -v  //用来编译树莓派可执行文件
 //二gcc是用来编译本地可执行文件
 //之所以是临时,是因为如果你再打开一个终端窗口,就木得了
2.永久有效:修改.bashrc 隐藏文件
修改工作目录下的.bashrc 隐藏文件,配置命令终端的
//在根目录下:cd
vi .bashrc   //或者
gedit .bashrc
//将:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/dazai/pi/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin
//放到106行的fi下面
//之后保存退出
source .bashrc   //使其生效
//之后再打开任何一个终端窗口都含有(认识):arm-linux-gnueabihf-gcc


So far, Raspberry Pi editor (cross-compilation tool chain), installation is complete.

3. Cross compile server client

A simple procedure:

#include <stdio.h>
int main()
{
    
    
	printf("hello\n");
	return 0;
}
gcc text.c -o text_1//编译
file text_1   //查看编译后的文件可执行在哪些系统上

Insert picture description here

How to put text.c on the Raspberry Pi to run, we use cross-compilation tools:

arm-linux-gnueabihf-gcc text.c -o text_2
file text_2

Insert picture description hereCopy the text_2 executable file to the Raspberry Pi operating system

Under linux terminal:

scp text_2 pi@192.168.1.100:   //注意: 后面跟路径,可以不写
//密码:raspberry

Under Raspberry Pi operating system:
Insert picture description here
Through the above cross-compilation, the program written by the host computer can be compiled into an executable program that can be run by the Raspberry Pi

Guess you like

Origin blog.csdn.net/weixin_40734514/article/details/108703397