Symbian
Symbian OS Library

SYMBIAN OS V9.3

[Index] [Spacer] [Previous] [Next]



HelloWorld: writes Hello World! to the console


Example Code

These are the main files contained in the examples. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

// HelloWorld.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

#include "CommonFramework.h"

// do the example
LOCAL_C void doExampleL()
    {
    _LIT(KHelloWorldText,"Hello world!\n");
    console->Printf(KHelloWorldText);
    }

// BLD.INF
// Component description file
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

PRJ_MMPFILES

HelloWorld.mmp

// HelloWorld.mmp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

// using relative paths for sourcepath and user includes

TARGET        HelloWorld.exe
TARGETTYPE    exe
UID           0
VENDORID 0x70000001

SOURCEPATH    .
SOURCE        HelloWorld.cpp

USERINCLUDE   .
USERINCLUDE   ..\CommonFramework
SYSTEMINCLUDE \Epoc32\include

LIBRARY       euser.lib

[Top]


Description

HelloWorld is a very simple piece of code showing how text is written to the console. All other examples in Basics and System use the same technique.

The file CommonFramework.h contains the entry point and all code necessary to support the example itself, including the construction of the console and the cleanup stack. Putting code into a .h file may not be conventional practice, but it is useful to do so here.

[Top]


Build

The source code for this example application can be found in the directory:

examples\Basics\HelloWorld

It may be in the directory in which you installed Symbian OS, or it may be in src\common\developerlibrary\. It includes the two project files needed for building: bld.inf and the .mmp file.

The Symbian OS build process describes how to build this application, which results in an executable called:

\epoc32\release\<target>\<urel or udeb>\HELLOWORLD.EXE.

[Top]


Usage

Run the executable HELLOWORLD.EXE.

Executables for the emulator targets wins and winscw can be run on your PC. Executables for ARM targets must be copied to your target platform before being run.

[Top]


Classes used

The example shows the use of the _LIT macro which is used throughout all of the examples. The macro generates a:

const static TLitC<TInt>

object and is an efficient and convenient way of generating constant literal text.

As this is a wide (Unicode) build, TLItC<TInt> is a typedef for TLitC16<TInt>.

[Top]


Example Code

HelloWorld.cpp

#include "CommonFramework.h"

// do the example
LOCAL_C void doExampleL()
    {
    _LIT(KHelloWorldText,"Hello world!\n");
    console->Printf(KHelloWorldText);
    }

CommonFramework.h

#ifndef __CommonFramework_H
#define __CommonFramework_H

#include <e32base.h>
#include <e32cons.h>

_LIT(KTxtEPOC32EX,"EXAMPLES");
_LIT(KTxtExampleCode,"Symbian OS Example Code");
_LIT(KFormatFailed,"failed: leave code=%d");
_LIT(KTxtOK,"ok");
_LIT(KTxtPressAnyKey," [press any key]");

// public
LOCAL_D CConsoleBase* console; // write all your messages to this
LOCAL_C void doExampleL(); // code this function for the real example

// private
LOCAL_C void callExampleL(); // initialize with cleanup stack, then do example

GLDEF_C TInt E32Main() // main function called by E32
    {
    __UHEAP_MARK;
    CTrapCleanup* cleanup=CTrapCleanup::New(); // get clean-up stack
    TRAPD(error,callExampleL()); // more initialization, then do example
    __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error));
    delete cleanup; // destroy clean-up stack
    __UHEAP_MARKEND;
    return 0; // and return
    }

LOCAL_C void callExampleL() // initialize and call example code under cleanup stack
    {
    console=Console::NewL(KTxtExampleCode,TSize(KConsFullScreen,KConsFullScreen));
    CleanupStack::PushL(console);
    TRAPD(error,doExampleL()); // perform example function
    if (error)
        console->Printf(KFormatFailed, error);
    else
        console->Printf(KTxtOK);
    console->Printf(KTxtPressAnyKey);
    console->Getch(); // get and ignore character
    CleanupStack::PopAndDestroy(); // close console
    }

#endif