Skip to content

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.

image-20220714151942356

We first create a new folder in the folder as a folder to store the project, named WorkSpcae

image-20220714154210921

Then create a new folder in the WorkSpace folder to store our helloworld code, named helloworld

image-20220714154536753

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

image-20220714154511488

Open the command line and enter the touch main.c command to create a new main.c source file.

image-20220714154712449

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;
}

image-20220714155106682

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.

image-20220714155441887

The gcc compiler used for compilation is in the toolchains/rootfsbuilt/arm/toolchain-sunxi-musl-gcc-830/toolchain/bin folder.

image-20220714155818227

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.

image-20220714160310369

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

image-20220714160532568

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

image-20220714160618124

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.

image-20220714161152580

Then you can see the card reader in the virtual machine.

image-20220714161502449

Copy helloworld into TF card

image-20220714161556372

Insert the TF card into the dev board

tf

ls /dev Check if mmcblk1 appears, you can see that there is mmcblk1 here

image-20220714161934483

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/

image-20220714162242302

Use the chmod command to give helloworld execute permission, run helloworld

chmod a+x helloworld
./helloworld

image-20220714162352478

You can see that Hello World is printed to the screen.