1. What is cross-compilation and why cross-compilation
What is it?
Cross-compilation is to generate executable code on one platform on another.
We write C51 code on windows and compile it into executable code, such as xx.hex,
which runs on c51, not on windows
We write Raspberry Pi code on ubuntu and compile it into executable code, such as a.out,
which runs on Raspberry Pi, not ubuntu linux
Compilation: is the executable code generated on a platform on that platform
C51 cross-compilation occurs in keil (above the integrated environment)
stm32
Why cross compile?
The compiler that we need is not allowed or cannot be installed
on the platform , such as C51 1. Because of the lack of resources on the target platform, we cannot run the compiler that we need.
2. Does the Raspberry Pi do not require cross-compilation?
wrong. Sometimes the Raspberry Pi is required because the target platform has not yet been established, and there is no operating system, and there is no compiler to run.
The operating system is also code and must be compiled!
Platform operation requires at least two things: 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: A system developed by a user, 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
-
Installation of cross compilation toolchain
https://github.com/raspberrypi/Copy from shared folder to working directory
cp /mnt/hgfs/share/tools-master.zip .
Unzip
unzip tools-master.zip
cd /home/CLC/lessonPI/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin
pwd get path
echo $PATH to get the value of the current environment variable
2.1 Temporarily effective, configure the environment variable
PATH environment variable
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/CLC/lessonPI/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin
2.2 Permanently effective, configure environment variables
Modify the hidden .bashrc file in the working directory, configure the command terminal
vi /home/CLC/.bashrc
Add in the last line of the file:
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/CLC/lessonPI/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin
Load the configuration file and the configuration will take effect immediately.
source /home/CLC/.bashrc
- Cross-compilation server client
Cross-compilation:
Check whether the cross-compilation tool is correct:
arm-linux-gnueabihf-gcc -v //4.8.3版本
4.8.3
arm-linux-gnueabihf-gcc xxx.c -o xxx
How to download the compiled executable file to the development board:
scp clientInPi pi@192.168.43.30:/home/pi
Instruction file name development board user name@development board address: the absolute path of the development board
-
How to carry out cross-compilation with wiringPi library
-
Normally, we need to cross-compile the wiringPi library first. The compiled library is suitable for Raspberry Pi. At this time, try to cross-compile the executable program. The format of the link library is also correct.
-
Specify by -I -L
Because the format of the linked library is wrong, it is the platform of the host machine, the following error occurs
-
arm-linux-gnueabihf-gcc demo2.c -I /home/CLC/lessonPI/WiringPi/wiringPi -lwiringPi
/home/CLC/lessonPI/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/../lib/gcc/arm-linux-gnueabihf/4.8.3/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lwiringPi
collect2: error: ld returned 1 exit status
Use the wringPI library of the Raspberry Pi
Soft link:
Reference article: https://www.cnblogs.com/zhangna1998517/p/11347364.html
Concept:
1. Soft link files have shortcuts similar to Windows.
2. In a symbolic link, the file is actually a text file, which contains the location information of another file.
3. Generate a mirror image of a file at the location you selected, which will not occupy disk space.
How to generate:
ln -s libwiringPi.so.2.50 libwiringPi.so
instruction parameter The name of the soft link file to be linked
Hard link: ln libwiringPi.so.2.50 libwiringPi.so
It will generate a file with the same size as the source file in the location you selected
After generating the soft link, execute the command:
arm-linux-gnueabihf-gcc demo.c -I /home/hhl/wiringPi/WiringPi/wiringPi -L. -lwiringPi -o
-L.用当前wiringPi 库