DESK-MX8M-L/Development/Application examples/Hello World C example

From DAVE Developer's Wiki
Jump to: navigation, search
History
Issue Date Notes

2022/02/17

First DESK-MX8M-L release

2023/02/24

DESK-MX8M-L-2.0.0 release
2023/08/22 DESK-MX8M-L-4.x.x release



Hello World C example[edit | edit source]

Here below an example on C code displaying the classic Hello World! message on the target serial console.

This example shows how to use the arm cross-compiler using the environment configured for this purpose

Setting the cross-compiler[edit | edit source]

  • start the Linux development VM and login into the system
  • open a terminal window and cd into your source code directory
dvdk@vagrant:~$ mkdir -p ~/myproject
dvdk@vagrant:~$ cd ~/myproject/
dvdk@vagrant:~/myproject$ vi hello.c
dvdk@vagrant:~/myproject$ cat hello.c 
#include <stdio.h>

int main(){
        printf("Hello, World!\n");
        return 0;
}
  • configure the build environment
dvdk@vagrant:~/myproject$ source /home/dvdk/desk-mx-l/desk-mx8m-l-4.0.0_env.sh
  • as you can see here below, the $CC environment variable has been properly configured for using the SDK sysroot parameter:
dvdk@vagrant:~/myproject$ echo $CC
aarch64-poky-linux-gcc -march=armv8-a+crc+crypto -fstack-protector-strong -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/opt/yocto/sdk/desk-mx8m-l/desk-mx8m-l-4.0.0/sysroots/armv8a-poky-linux
dvdk@vagrant:~/myproject$ 
  • invoke the cross-compiler for compiling your source code example: the object file obtained, is a proper ELF 64-bit for the target microprocessor
dvdk@vagrant:~/myproject$ $CC hello.c -o hello
dvdk@vagrant:~/myproject$ file hello
hello: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, BuildID[sha1]=a7220d78ac1016774b614ce1d50adae71cd713a3, for GNU/Linux 3.14.0, with debug_info, not stripped
dvdk@vagrant:~/myproject$

Running the example on the target[edit | edit source]

Now it is enough to copy the object file in the /home/root rfs directory and boot from nfs...

dvdk@vagrant:~/myproject$ sudo cp hello /home/dvdk/desk-mx-l/rfs/desk-mx8m-l-4.0.0/mx8mp/home/root/
dvdk@vagrant:~/myproject$ sudo ls -la /home/dvdk/desk-mx-l/rfs/desk-mx8m-l-4.0.0/mx8mp/home/root/
total 32
drwx------ 3 root root  4096 Jul 25 12:52 .
drwxr-xr-x 4 root root  4096 Mar  9  2018 ..
-rwxr-xr-x 1 root root 13928 Jul 25 12:52 hello
drwxr-xr-x 7 root root  4096 Mar  9  2018 .nxp-demo-experience
-rw-r--r-- 1 root root  1011 Mar  9  2018 .profile
dvdk@vagrant:~/myproject$ 

or copy the cross-compiled executable via scp directly in the target:

dvdk@vagrant:~$ scp hello root@192.168.0.89:/home/root
The authenticity of host '192.168.0.89 (192.168.0.89)' can't be established.
ECDSA key fingerprint is SHA256:B9mctvtBzEGWLWtmbjuH4LkHDxoNPzLF1PzhGLtFvL0.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.0.89' (ECDSA) to the list of known hosts.
hello                                                                                                                                                      100%   14KB   1.9MB/s   00:00    
dvdk@vagrant:~$ 

Then, execute the hello in the target:

...
...
...
[  OK  ] Reached target Multi-User System.
[  OK  ] Started Weston, a Wayland .|mpositor, as a system service.
[  OK  ] Reached target Graphical Interface.
         Starting Record Runlevel Change in UTMP...
[  OK  ] Started Hostname Service.
[  OK  ] Started WPA supplicant.
[  OK  ] Finished Record Runlevel Change in UTMP.

NXP i.MX Release Distro 5.15-kirkstone desk-mx8mp ttymxc1

desk-mx8mp login: root
root@desk-mx8mp:~#
root@desk-mx8mp:~# ls -la
total 44
drwx------ 3 root root  4096 Jul 25 10:56 .
drwxr-xr-x 4 root root  4096 Mar  9  2018 ..
-rw------- 1 root root  8362 Jul 25 10:53 .bash_history
drwxr-xr-x 7 root root  4096 Mar  9  2018 .nxp-demo-experience
-rw-r--r-- 1 root root  1011 Mar  9  2018 .profile
-rwxr-xr-x 1 root root 13928 Jul 25 10:55 hello
root@desk-mx8mp:~# ./hello 
Hello, World!
root@desk-mx8mp:~#