Hello World
This guide will walkthrough the creation and compilation of a Hello World executable intended to run directly on the phyCORE-i.MX7 Development Kit running Linux.
Native Compilation (On the Target)
Target Image Setup
By default, the BSP-Yocto-NXP-i.MX7-PD23.1.2 phytec-qt6demo-image doesn’t include build tools such as gcc, make, git, etc. In order to build your applications on the target natively, these packages will have to be added to your target image first.
First, complete the setup steps outlined in the Build the BSP guide.
Once setup, modify your $BUILDDIR/conf/local.conf:
vi $BUILDDIR/conf/local.conf
Add the following line to the end of the file:
$BUILDDIR/conf/local.conf
IMAGE_INSTALL:append = " packagegroup-core-buildessential"
Now rebuild your target image and then use it to boot your phyCORE-i.MX7 Development Kit into Linux.
host:~$ bitbake phytec-qt6demo-image
Write your HelloWorld Code
Let’s make a project directory to contain the Hello World source code:
phyboard-zeta-imx7d-1:~# mkdir ~/helloworld-project
phyboard-zeta-imx7d-1:~# cd ~/helloworld-project
Create the main Hello World application source code file using your favorite text editor, this guide will leverage ‘vi’ but ‘nano’ is a more beginner friendly option:
phyboard-zeta-imx7d-1:~# vi helloworld.c
Note
The vi text editor begins in “Command Mode” and you must first hit the ‘i’ key in order to enter “Insert Mode”. Using the arrow keys to navigate, make the necessary changes and then hit ESC to go back to “Command mode”. Now enter “:wq” to write the file and quit.
Pro Tip: Use the right click on your mouse to paste! This will only work if you are in “Insert Mode” first.
Edit the contents of the file to reflect the following and remember to save your changes when you are done!
#include <stdio.h>
int main()
{
printf("Hello World!\n");
}
Compile the project.
phyboard-zeta-imx7d-1:~# gcc -O helloworld.c -o helloworld
Run the binary:
phyboard-zeta-imx7d-1:~# ./helloworld
You should see the following output:
phyboard-zeta-imx7d-1:~# ./helloworld
Hello World!
Cross-Compilation (On a Linux Host Machine)
Once you follow the Build the BSP guide to help you build a SDK installer (or install a pre-built one), and then run through the Install The SDK guide to install it and then source it, you can leverage the computing resources and development tools available to your Linux Host Machine to compile applications intended to run on the phyCORE-i.MX7 Development Kit.
Basically, you can create the helloworld.c file using the same steps above using your Linux Host Machine’s Terminal and then use the cross-compilation toolchain to compile your application to run on a different architecture (in this case it would be the ARM architecture):
$CC -O helloworld.c -o helloworld
Once built, you can confirm the target architecture the binary is intended for using the ‘file’ utility:
host:~$ file helloworld
hello: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=dc8a85866d965e6a45eff08156df82c14faac5db, for GNU/Linux 3.2.0, with debug_info, not stripped
You’ll notice errors if you try to run this binary on the Host Machine. Check out the Copying Files to the Device Guide to help you transfer it to the phyCORE-i.MX7 Development Kit such that you can execute it.