Native C "Hello World" for LTPS

Before running this example, please make sure that you've correctly set up the SDK environment.

Write a Simple Program

[dv@dvh tmp]$ mkdir test_0
[dv@dvh tmp]$ cd test_0/
[dv@dvh test_0]$ touch ./test_0.c
  • Create test_0.c file with the following contents:
#include <stdio.h>

int main( int argc, char *argv[]) {
 printf( "Hello world from LTPS!\n");
 return( 0);  }
  • Now create Makefile for this program. This is an old, traditional way of building anything in UNIX:
[dv@dvh test_0]$ touch ./Makefile
[dv@dvh test_0]$ vi ./Makefile
  • Makefile contents:

There are two tabs (and not spaces) at the beginning of each indented line.

all:
        $(CC) -o test_0 test_0.c

clean:
        rm -f test_0
        rm -f *.o

If you're not familiar with the syntax, please read the Makefile syntax article.

Build Your Program with a Cross-compiler

  • Run the make command in the same terminal emulator window where you previously ran LTPS SDK environment setup script.
[dv@dvh test_0]$ make
arm-tps-linux-gnueabi-gcc  -march=armv7-a -mfpu=neon  -mfloat-abi=hard -mcpu=cortex-a8 --sysroot=/home/dv/tpsC/sysroots/cortexa8hf-neon-tps-linux-gnueabi -o test_0 test_0.c
[dv@dvh test_0]$ ls -l
total 20
-rw-r--r-- 1 dv dv   64 May 16 22:28 Makefile
-rwxr-xr-x 1 dv dv 9144 May 16 22:28 test_0*
-rw-r--r-- 1 dv dv  110 May 16 22:27 test_0.c

The compiler will use instructions in the Makefile and build the test_0 executable binary from the test_0.c source. If you try to run it now, it will fail with the "cannot execute the binary file" message.

[dv@dvh test_0]$ ./test_0
bash: ./test_0: cannot execute binary file

Why is it so? Because the code was compiled for a non-x86 CPU! Let's test it with the objdump compiler utility from your native gcc tools and from the LTPS SDK:

[dv@dvh test_0]$ objdump -f ./test_0

./test_0:     file format elf32-little
architecture: UNKNOWN!, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x000102f4

[dv@dvh test_0]$ $OBJDUMP -f ./test_0

./test_0:     file format elf32-littlearm
architecture: arm, flags 0x00000112:
EXEC_P, HAS_SYMS, D_PAGED
start address 0x000102f4

We have tested this with two different objdump utilities: a traditional x86 objdump that can be found in the default $PATH and a cross-objdump from the LTPS SDK (see the $OBJDUMP environment variable).

The first (Intel) objdump reports that it can't detect the architecture. Cross-objdump says that this is a 32-bit little-endian ARM executable. This is correct!

[dv@dvh test_0]$ scp ./test_0 root@192.168.75.217:~/
root@192.168.75.217's password:
test_0                                        100% 9144     8.9KB/s   00:00
[dv@dvh test_0]$ ssh root@192.168.75.217
root@192.168.75.217's password:
X11 forwarding request failed on channel 0
root@tpp:~# ./test_0
Hello world from LTPS!
root@tpp:~# uname -a
Linux tpp 4.4.3-tpp #1 Sat May 14 14:28:50 MSK 2016 armv7l GNU/Linux
root@tpp:~#

Success!