Simple Hello World !!

In my previous tutorial i have shown how to setup Virtual Embedded Linux System using Qemu. In this tutorial we will try to explore how to write a simple Embedded Application, cross-compiling and finally running on target.

When an application  runs on the target which is similar to your desktop machine which has a Root file system  and  the same set of device nodes as that of Target. These similarities make it possible to use your desktop machine as a reasonable emulator for your target board.

Though the processor is different, but between the kernel and the development language, the difference apparent to you is small to nonexistent, depending on the nature of the application. There are some key differences, however, which fall into two large categories, language and system related.

Language-related :- Differences appear as a result of the development language used to build the code.
system-related :- Differences have to do with the Linux kernel and root file system.

The target might be slower than, less memory than, smaller fixed storage than, and probably doesn’t have the same input and output features as the desktop.The Linux kernel provides a uniform interface to resources that stays the same even if the kernel is running on a different processor. Similarly, when programming in C, you use the same function calls for printing, reading input, interprocess communication, and process control. Compiling the C code with a cross-compiler outputs different object code, which, when run on the target, should work the same as the code run on the development host.

When you’re doing application development, the best strategy is to compile the code for execution on your development host and work out coding problems that aren’t architecture-specific before you test the code on the target. In order to do this, you must do some work to compile the code so it runs on the host and the target.

Let us begin with a simple hello World Program .
Before getting started, you need the following:

  •  A cross-compiler: 2014.05-29-arm-none-linux-gnueabi-i686-pc-linux gnu
  •  A target to run what’s been compiled: This can be the board or an emulated board
    on your development host. In our case Qemu
  •  A way to get the code on the target: After the code is compiled, you can’t run it on
    your target machine, so you need a way to get the binaries to the target. Here we will use NFS Rootfile system .
#include < stdio.h >;
int main(int argc, char** argv)
{
 printf("Hello All, Welcome to Embedded Linux \n");
 return 0;
}

Save the above file as Hello.c

Compile the program using below command.

arm-none-linux-gnueabi-gcc Hello.c -o Hello

file Hello

Hello: ELF 32-bit LSB&amp;nbsp; executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, not stripped

This tells you that the code is built for a  ARM processor, uses shared libraries, and isn’t stripped of debugging information.

Copy the Hello Executable to your NFS rootfs  /Home directory . Once copy is done run below command .

qemu-system-arm -M vexpress-a9 -dtb linux/arch/arm/boot/dts/vexpress-v2p-ca9.dtb -kernel linux/arch/arm/boot/zImage -append "root=/dev/nfs nfsroot=10.0.2.2:/NFS_rootfs rw ip=10.0.2.15::10.0.2.1:255.255.255.0 init=/linuxrc console=ttyAMA0" -serial stdio

hello

Congratulations! You’ve run the program on the target.

The above holds good only when you compiled Busybox as Dynamic and shared library.     i.e [ ] Build BusyBox as a static binary (no shared libs) . This option as unchecked and application by default is compiled dynamically,  can be verified with “file” command.

Suppose if you have compiled busybox as Static binary option then you  need to compile Hello application using  static flag option.

arm-none-linux-gnueabi-gcc -static Hello.c -o Hello

Let me know if you have any questions or comments.

Reference:-

1)Pro Embedded Linux by Sally Gene

Advertisements

About VinayMahadev

I am passionate about Embedded Linux systems . I believe in "If you want to learn something, read about it. If you want to understand something, write about it. If you want to master something, teach it". Here I am just trying to connect the Dots.
Gallery | This entry was posted in C, Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s