Hello World
This chapter will explain how to use a computer (host computer) to cross compile a small application that prints Hello Word
, upload it to the development board (host computer), run it, and print out hello word
.
Prepare the Build Environment
Here we use the virtual machine installed in the Compile Environment Configuration chapter for demonstration. This virtual machine has been configured with some basic compilation tools and cross-compilation toolchain, which is more convenient to use.
We first create a new folder in the folder as a folder to store the project, named WorkSpcae
Then create a new folder in the WorkSpace
folder to store our helloworld code, named helloworld
At this point, the environment for compiling the helloworld project is ready.
Wirte HelloWorld Code
Go to the helloworld
folder, right click on the blank space to see the Open in Terminal
option
Open the command line and enter the touch main.c
command to create a new main.c
source file.
Double-click to open the main.c
source file, write the source code and save it
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Hello World\n");
return 0;
}
Cross-compilation
Cross-compilation refers to compiling executable program files that can be run on the development board on our PC. Because it is compiled on the host computer and then run on boards of different architectures, it is called cross-compilation.
Before cross-compilation, you should know
The cross-compilation toolchain used for cross-compilation is named toolchain-sunxi-musl-gcc-830
and is stored in the toolchains\rootfsbuild\arm\
folder.
The gcc
compiler used for compilation is in the toolchains/rootfsbuilt/arm/toolchain-sunxi-musl-gcc-830/toolchain/bin
folder.
Of course, a compiler is not enough, you also need to provide those library files and header files that you need to use when compiling. These files are stored in the toolchains/rootfsbuilt/arm/toolchain-sunxi-musl-gcc-830/toolchain/arm-openwrt-linux-muslgnueabi
folder.
Start Cross-compilation
First, specify the folder where the header files of the library files needed for cross-compilation are stored.
export STAGING_DIR=~/toolchains/rootfsbuilt/arm/toolchain-sunxi-musl-gcc-830/toolchain/arm-openwrt-linux-muslgnueabi
Then you can use the cross-compilation toolchain to compile, and you can see that the compiled helloworld
executable file has been generated.
~/toolchains/rootfsbuilt/arm/toolchain-sunxi-musl-gcc-830/toolchain/bin/arm-openwrt-linux-gcc -o helloworld main.c
Run in Board
We can upload files to the dev board using various methods such as U disk, adb, nfs, TF card, etc. Here we use the TF card to upload the helloworld
executable file to the development board.
First, insert the TF card into the card reader and connect it to the computer. In the device, you can see a Mass Storage device in the USB. Check this device.
Then you can see the card reader in the virtual machine.
Copy helloworld
into TF card
Insert the TF card into the dev board
ls /dev
Check if mmcblk1
appears, you can see that there is mmcblk1
here
Then mount the TF card, the partition is mmcblk1p1
, mount it to the /mnt/SDCARD
path, then cd
into the /mnt/SDCARD
folder, you can use the ls
command to list the files, You can see that helloworld
is here.
mount /dev/mmcblk1p1 /mnt/SDCARD/
Use the chmod
command to give helloworld
execute permission, run helloworld
chmod a+x helloworld
./helloworld
You can see that Hello World is printed to the screen.