Listing 131 Helloc

#include <stdio.h> <—O Standard include file int main(int argc,char * argv [] ) <1—© Application entry point {

printf ("Hello, Android! \n") ; <1—© Display a string return 0;

Virtually all C language applications require a #include header file containing function definitions, commonly referred to as prototypes. In this case, the application includes the header file O for the standard input and output routines, stdio.h. The standard C language entry point for user code © is the function named main. The function returns an integer return code (a value of zero is returned in this simple example) and takes two arguments. The first, argc, is an integer indicating the number of command-line arguments passed in to the program when invoked. The second, argv, is an array of pointers to null-terminated strings representing each of the command-line arguments. The first argument, argv[0], is always the name of the program executing. This application has but a single useful instruction, printf, which is to write to standard output (the screen) a textual string Q. The printf function is declared in the header file, stdio.h.

To build this application, we employ the gcc tool:

arm-none-linux-gnueabi-gcc hello.c -static -o hellostatic There are a few items to note about this command-line instruction:

■ The compiler is invoked with the full name arm-none-linux-gnueabi-gcc.

■ The source file is named hello.c.

■ The -static command-line switch is used to instruct gcc to fully link all required routines and data into the resulting binary application file. In essence, the application is fully standalone and ready to be run on the target Android Emulator without any additional components. An application that's statically linked tends to be rather large because so much code and data are included in the executable file. For example, this statically linked application with basically a single line of code weighs in at 568,231 bytes. Ouch! If this -static switch is omitted, the application is built without any extra routines linked in. In this case the application will be much smaller; however, it will rely on finding compatible routines on the target system in order to run. For now, we are keeping things simple and building our sample application in such a manner that all support routines are linked statically.

■ The output switch, -o, is used to request the name of the executable application to be hellostatic. If this switch is not provided, the default application name is a.out.

Now that the application is built, it's time to try it out on the Android Emulator. In order to do this we will rely on the adb tool introduced in chapter 2.

0 0

Post a comment