3.8. First “Hello World” Program

Application development can begin once Linux boots up and runs. This section describes how to compile a simple “Hello world” C program to run on the target. The compilation can be done either on the host or on the target.

3.8.1. Cross Compilation on the Host

This section demonstrates how to do cross compilation on the host, assuming the Processor SDK Linux has already been installed on the development host.

The Processor SDK contains an ARM-based toolchain which can be used to cross-compile C programs to run on the target. The SDK has a script to set up the development environment including the toolchain paths on the host. This script can be sourced as shown below:

$ source <processor sdk installation root>/linux-devkit/environment-setup-aarch64-linux

After running this script, the compiler is specified by the environment variable CC.

Create a simple C program to print “Hello world”. For example:

#include <stdio.h>

int main()
{
  printf("Hello world from TI Linux!\n");
  return 0;
}

Save this program to a file (e.g., hello_world.c), and cross-compile the program using the SDK toolchain:

$ ${CC} hello_world.c -o hello_world

The binary file generated above can run on the target. Copy this file to the target using the scp command (or simply copy it to the target file system if NFS is used):

$ scp hello_world root@<target ip address>:~/

On the target, go to the home folder. Type “hello_world”. The program should run and print out the following:

Hello world from TI Linux!

3.8.2. Native Compilation on the Target

This same “Hello world” C program can also be natively compiled on the target:

# gcc hello_world.c -o hello_world
# ./hello_world
Hello world from TI Linux!

Native compilation is simple and straightforward, without having to set up the environment and move the binary from the host to the target. On the other hand, cross compilation on the host can take advantage of more advanced development tools and usually higher processing power.