Lesson 4: HelloWorld on Elastos

Overview

 * Write the good old C version of "Hello World" on Elastos.
 * Write the recommended version of "Hello World" on Elastos
 * How to build and run C programs on Elastos.

A pure C/C++ program
"Those who cannot remember the past are condemned to repeat it." (George Santayana, 1905)

There are several ways to execute a new program in Elastos. For example, the traditional C/C++ method of execution still works, i.e., We still support the following for backward compatibility. int main(int argc, char* argv[]);

int wmain(int argc, wchar_t* wargv[]); The main and wmain functions will exit a program upon returning from the main body of code. This is the way it has been for more than 30 years, and we have no intention of changing that. You can find the HelloWorld1 sample source code at ~/ElastosRDKforUbuntu/Sources/Elastos/Samples/GettingStarted. HelloWorld1.cpp
 * 1) include 

int main {   printf("Hello World!\n"); return 0; } The manifest file: sources TARGET_NAME= HelloWorld1 TARGET_TYPE= ecx

SOURCES = HelloWorld1.cpp

LIBRARIES = $(XDK_USER_LIB)/Elastos.Runtime.lib LIBRARIES += -lstdc++ -lm -ldl -llog -lz -lcutils -lutils -lbinder
 * TARGET_TYPE= ecx:Informs the compiler and compiles the program into an executable file that runs on Elastos.
 * Elastos.Runtime.lib: The Elastos’s runtime library. All programs running on Elastos should load it first.

The recommended version of "Hello World" on Elastos
The old LibC has two major problems:
 * Prone to virus attacks (due to buffer overrun);
 * Lack of internationalization support.

Elastos, however, introduces two more ways of starting up a program: one for the Elastos EXE file, and the for the Elastos DLL module. An EXE version of the Elastos source code should contain the HelloWorld2 function. You can find the HelloWorld2 sample source code in the ~/ElastosRDKforUbuntu/Sources/Elastos/Samples/GettingStarted. ECode ElastosMain(const BufferOf& args) The ElastosMain function has only one argument because BufferOf is self-describing and has the number of elements built-in. After returning from the ElastosMain function successfully, the execution flow will fall into an event processing loop, similar to what Windows-Message-Loop does. Here is the recommended version of "Hello World" on Elastos: HelloWorld2.cpp using namespace Elastos;
 * 1) include 
 * 2) include 

ECode ElastosMain(const BufferOf& args) {   printf("Hello World!\n"); return NOERROR_EXIT; }
 * elastos.h:The only header you need to remember for Elastos APIs.


 * ElastosMain:Entry point for the main Applet thread.


 * Return NOERROR_EXIT;:Entry point for the Applet's event loop.

Sources TARGET_NAME= HelloWorld2 TARGET_TYPE= ecx

SOURCES = HelloWorld2.cpp

LIBRARIES = $(XDK_USER_LIB)/Elastos.Runtime.CarRuntime.lib LIBRARIES += $(XDK_LIB_PATH)/Elastos.Runtime.lib Elastos.Runtime.CarRuntime.lib   The CAR runtime library. All programs that are written in CAR and run on Elastos should load it first.

Useful Commands

 * (1) emake: Build target files in the directory(recursively)
 * (2) emake clean:Clean up the target files in the mirror directory
 * (3) emake clobber:Clean up the whole project in the target directory
 * (4) pd @:Push current directory onto command stack, then jump to the target directory.
 * (5) pd:Pop command stack, i.e., jump back to the previously working directory.
 * (6) pd ~:Push current directory onto command stack,then jump to the mirror directory (of built files).
 * (7) ./HelloWorld1:Execute the HelloWorld1 demo
 * (8) elcopy:Copy a file or a directory to device
 * (9) eldrop:Copy all the elastos runtime files to device
 * (10) elrsh:Start the adb shell

Tips

 * (1) The symbol "@" resembles a "target" in the real world.
 * (2) The symbol "~" resembles a "distorted mirror" in the real world.

Build and run HelloWorld1
Step1: ~/ElastosRDKforUbuntu/Sources/Elastos/Samples/GettingStarted/HelloWorld1$emake or emake rebuild

About the make compilation process: If there exists a dirs file in the current directory, emake will compile all the directories recursively and sequencely that are listed in the dirs. After that, if there exists a sources file in the current directory, emake will compile the project files according to the sources file. After finishing the work assigned in the sources file, if there exists a makefile.inc file, emake will deal with the work assigned in the makefile.inc file. If any errors are encountered during the process, emake will stop compiling and display error informations.

For more infomation on dirs, sources, and the makefile .inc file, please refer to the the relevant documents.

Step2: pd @ change to ~/ElastosRDKforUbuntu/Targets/rdk/arm.gnu.linux.dbg/bin emu -c elrsh

Step3: ~/ElastosRDKforUbuntu/Targets/rdk/arm.gnu.linux.dbg/bin$elcopy HelloWorld1 Copying HelloWorld1 to /data/data/com.elastos.runtime/elastos... 59 KB/s (2723 bytes in 0.044s)

Step4: in adb shell: cd data/data/com.elastos.runtime ls: To confirm all the files and directory are available.
 * 1) ./HelloWorld1

Result: Hello World!

Back