Search by Tags

Build from Command-line

 
 

Overview

In this section, you will build a simple hello-world using the previously configured Software Development Kit (SDK).

In this module you will:

  • Write a hello-world
  • Cross-compile from command-line
  • Deploy to the target through the network

Step 1

Change Ubuntu shell from dash to bash:

sudo update-alternatives --install /bin/sh sh /bin/bash 100

Skipping this step might break instructions ahead. Still you can have both dash and bash installed if you wish. To do so, please run the following commands to install dash as an alternative and select bash as the current shell:

sudo update-alternatives --install /bin/sh sh /bin/dash 50
sudo update-alternatives --config sh

Step 2

Create a file named hello-world.c with the following contents:

hello-world.c
#include <stdio.h> int main(int argc, char *argv[]){ printf("Hello world!\n"); return 0; }

Step 3

Cross-compile the code. Make sure that you exported the required variables in the previous lesson.

Note: The Toradex SDK must be installed in the system and the correct variables exported. If you have any doubts, please go back to the previous lesson Configure Toolchain.

Warning: If you open a new terminal window, you must export the variables again. To check if a variable is set, use the echo command, for instance: "echo ${CC}".

${CC} -Wall hello-world.c -o hello-world

Step 4

Log in to the board using minicom.

Note: Minicom usage is described in the Module 1 - Linux Terminal and Basic Usage lesson of this getting-started guide.

Find the target IP address. Search for the inet addr from the eth0 interface:

root@computer-on-module:~# ifconfig 
eth0      Link encap:Ethernet  HWaddr 00:14:2D:49:E8:8E  
          inet addr:192.168.0.21  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::214:2dff:fe49:e88e/64 Scope:Link
          inet6 addr: 2804:14d:ac82:116c:214:2dff:fe49:e88e/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:699 errors:0 dropped:0 overruns:0 frame:0
          TX packets:562 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:86400 (84.3 KiB)  TX bytes:56325 (55.0 KiB)
 
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:330 errors:0 dropped:0 overruns:0 frame:0
          TX packets:330 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:30218 (29.5 KiB)  TX bytes:30218 (29.5 KiB)
 
usb0      Link encap:Ethernet  HWaddr 00:14:2D:FF:FF:FF  
          inet addr:192.168.11.1  Bcast:192.168.11.255  Mask:255.255.255.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

In this example, the target IP address is 192.168.0.21.

Step 5

Back to the host computer, copy the application to the target using SCP. You may be prompted to continue during the first connection. If this is the case, just type yes and press Enter:

user@host:~$ scp hello-world root@192.168.0.21:/home/root
The authenticity of host '192.168.0.21 (192.168.0.21)' can't be established.
ECDSA key fingerprint is SHA256:+N5WCsnEtQXEnYWyMXxswZLf4hEVgmHPOR2pZgJRdrg.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.21' (ECDSA) to the list of known hosts.

Warning: The board and the host machine must be connected to the same network. Please refer to the Module 1 - Unboxing and Setup Cables lesson for more information about the system setup.

Step 6

In the target terminal, run the application:

root@computer-on-module:~# ./hello-world 
Hello world!