Lesson 3: CAR Component Structure and Compiling

Overview

 * The structure of a CAR component
 * How to define a CAR component
 * How to get C/C++ code skeleton by compiling a *.car file

CAR file
A CAR file is either defined by a keyword module or a library. A CAR file mainly includes classes, interfaces and interface function. The CAR file shown above can be written with the following CAR grammar: module Hello.car {   interface IHello1 { Hello; }   interface IHello2 { Hello; }   class CHello1 { interface IHello1; }   class CHello2 { interface IHello2; } }

CAR Interface
An interface is a set of logically related functions, while those individual functions are known as interface member functions. CAR interface names must start with "I"; if not, the CAR compiler will return an error. CAR interfaces do not implement functions; they only define them. The associated class declared in the CAR file will implement the interface functions. For more details about using interfaces, please refer to the "interface" keyword chapter.

CAR Class
Similar to C++ objects, a CAR object is an instance of a class, where a class is a construct composed of a group of related data and functions. CAR classes implement the interface functions, so that the CAR object can call the functions through these interfaces.

CAR Objects
The CAR system automatically provides a CAR API named "New" for each CAR class, in order to create CAR object instances. When calling this function to create an object, developers access one of the interfaces of the object (and the reference count of users for that component is incremented). The interface can then be used to call all of its member functions, and must be be released after usage.

In the CAR model, the object itself is not visible to the customer. Developers can only request services through CAR interfaces, whose members functions are linked with CAR objects.

Compiling of a CAR file
The process of compiling a CAR file can be divided into four steps. The first step is for the developer to actually write the CAR file by the developer. The second step is to execute the command "emake xxx.car" in the console (or through Elastos Studio), so the CAR Compiler can generate a set of files in the user’s path. The third step is to implement the code files generated by the CAR Compiler. Finally, the last step is to execute the “emake” command in the console (or through Elastos Studio). The CAR Compiler will generate another set of intermediate code files in the system path, compiling and linking them into an eco or lib file.

The CAR compiler will generate two sets of code files, both in the user path and in the system path. Files generated by the CAR Compiler in the user’s path include：
 * xxx.h:Generated header file for CAR classes
 * xxx.cpp:Generated implementation file for CAR classes
 * sources:Project manifest file

The .cpp and header files above are implemented by the developers according to their requirements. The intermediate code files generated in the system path provide services for the developers transparently. Their detailed contents don’t have to be known. Picture：Compile procedure

emake
Elastos RDK provides a compiler tool for developers who want to create the code skeletons of a CAR component from a CAR definition file(*.car), or who want to build their Elastos projects. The compiler is accessed from the command line through the term "emake". Emake command contains two compile modes. One is for the creation of the code skeletions from a *.car file. The other is for compiling complete Elastos projects. The compiler will compile all the files in the project root directory and its sub-directories recursively according to the manifest files such as dirs and sources files ( such as dirs and sources files).

Command Format

emake [-? | -v | all | clean | clobber | rebuild | carfile.car]

Functions
 * -?:Display help information
 * -v:Display detailed compile information during compile time
 * all:Compile all files in the current directory and sub-directories recursively
 * clean:Delete the images of the current directory which is generated in the mirror directory.
 * clobber:Delete the whole mirror directory and the target directory over the entir all the current development environment.
 * rebuild:Equivalent to the combination of emake, clean, and emake.
 * .car:Create the set of code skeletons of a CAR component from a CAR definition file(*.car)

Exercise: Get a C/C++ code skeleton by compiling a *.car file
1. Create a new directory under ~/Elastos/Samples.

Switch into the ~/Elastos/Samples directory.

Create a new directory named Hello'.

2. Create and define the Hello.car file.

The content of the Hello.car file is shown below.

Hello.car //========================================================================== // Copyright (c) 2000-2009, Elastos, Inc. All Rights Reserved. //==========================================================================

module {   interface IHello1 { Hello1; }

interface IHello2 { Hello2; }

class CHello { interface IHello1; interface IHello2; } }

3. Open ARM Android SDK tool environment.

4. Switch into ~/Elastos/Samples/Hello

5. emake Hello.car -After emake finishes successfully, three new files will be created automatically in the same directory. ECode CHello::Hello1 {   // TODO: Add your code here return E_NOT_IMPLEMENTED; }
 * Hello.cpp
 * 1) include "CHello.h"

ECode CHello::Hello2 {   // TODO: Add your code here return E_NOT_IMPLEMENTED; }


 * Hello.h
 * 1) ifndef __CHELLO_H__
 * 2) define __CHELLO_H__


 * 1) include "_CHello.h"

CarClass(CHello) { public: CARAPI Hello1;

CARAPI Hello2;

private: // TODO: Add your private member variables here. };

TARGET_NAME= Hello TARGET_TYPE= eco
 * 1) endif // __CHELLO_H__
 * sources

SOURCES = Hello.car SOURCES += CHello.cpp

LIBRARIES= $(XDK_LIB_PATH)/Elastos.Runtime.CarRuntime.lib LIBRARIES+= $(XDK_LIB_PATH)/Elastos.Runtime.lib LIBRARIES += $(XDK_LIB_PATH)/Elastos.Utility.lib LIBRARIES += $(XDK_TARGETS)/Elastos.Runtime.eco LIBRARIES += $(XDK_TARGETS)/Elastos.Utility.eco

Back