S60 Open C
Introduction to Open C

Introduction to Open C

Table of Contents

Getting started with Open C
Changes to the MMP file
Example using E32Main()
Example using main()
Hybrid application with both Symbian C++ and standard C codes and files
Using Open C++ on Carbide
Creating Hello World project
Deleting Hello World Project
Importing Hello World project in Carbide C++
Building Hello World Project
Changing the build configuration

 


Getting started with Open C

 


Changes to the MMP file

Add needed libraries used by the MMP file structure:

If developers want to use any of the Open C library, they need to link to the corresponding library in the MMP file using theLIBRARY keyword.

If the application has main() as the entry point, the library libcrt0.lib must be specified as the first library otherwise, it will result in linker errors. The user must link to the Symbian OS euser.dll. This is required since the static library uses some of the services of the Symbian OS such as creating cleanup stack, and having a top level TRAP. All these details are hidden from the developer. The developer will write the application as if it were for the UNIX environment.

STATICLIBRARY  libcrt0.lib
LIBRARY        libc.lib 
LIBRARY        euser.lib  // Needed in order to use Symbian services
// and whatever Open C libraries are needed…

Thelibcrt0.lib library is required if the user is not going to write E32Main within the application (EXE). This static library has an implementation of E32Main within which it calls the library initialization method followed by calling main written by the developer. This static library also gets command-line arguments and passes the same to main.

If the application has E32Main() as an entry point, there is no need to link to libcrt0.lib like in the example below.

LIBRARY         libc.lib 
LIBRARY         libm.lib libpthread.lib 
LIBRARY         euser.lib

Add needed include paths

SYSTEMINCLUDE   \epoc32\include\stdapis 
Note: Some of the SSL/cryptography functions need more than the default available stack. The recommended stack size is 10K. To set the stack size to 10K add
EPOCSTACKSIZE 0x10000
in the MMP file.

 


Example using E32Main()

A simple example using E32Main() as an entry point is described below. The example writes a text to a file.

  • Modify the MMP file as mentioned earlier.
  • Create a trap handler using CTrapCleanup.
  • Call the method within TRAPD.
  • Delete the trap handler.
#include <stdio.h>
#include <string.h>
#include <e32base.h>

void doExampleL(void)
{
    FILE* fd;
    char* fileName = "C:\\test.txt";
    char *buf = "Hello world from E32Main()";
    fd = fopen(fileName, "w");
    if (fd == NULL)
    {
        printf("Unable to open the file (%s)", fileName);
        return;
    }
    if (fwrite(buf, sizeof(char), strlen(buf), fd) < 0 )
    {
        perror("write fails.");
    }
    fclose(fd);
}

GLDEF_C TInt E32Main()
    {
    CTrapCleanup* cleanup=CTrapCleanup::New();
    TRAPD(error,doExampleL());
    delete cleanup; // destroy cleanup stack
    return 0; // and return
    }

 


Example using main()

A simple example using main() as an entry point is described below. The example writes a text to a file.

  • Modify the MMP file as mentioned before.
  • Do usual C style coding.
#include <stdio.h>
#include <string.h>

int main(void)
{
    FILE* fd;
    char* fileName = "C:\\test.txt";
    char *buf = "Hello world";
    fd = fopen(fileName, "w");
    if(fd == NULL)
		{
		printf("Unable to open the file (%s)", fileName);
		return -1;
		}
    if (fwrite(buf, sizeof(char), strlen(buf), fd) < 0 )
        {
    	perror("write fails.");
        }
    printf("File (%s) is created successfully.", fileName);
    fclose(fd);
    getchar();
    return 0;
	}

 


Hybrid application with both Symbian C++ and standard C codes and files

If the developers want to use any of the Open C STDLIB while writing some Symbian application, they need to link to the corresponding library. It is same as in the case above, the only difference being that the developer does not have to link with the STATICLIBRARY libcrt0.lib in the MMP file.

There are no additional Open C-specific changes that need to be done in the source file in this case. This is possible, because the developer of Open C does not have to call any library initialization routine before use or cleanup routine after use.

Some of the Open C STDLIBS APIs assume that a cleanup stack is created and there is a top-level TRAP. If the developer does not create either of the two, calling such API’s may lead to application crash.

NOTE! In a hybrid application, the user must make sure that the cleanup stack is created and a top-level TRAP marker is done.

Hybrid application can also contain threads created using both RThread::Create and pthread_create, and can still use Open C APIs in both the threads. In case of RThread::Create the developer has to create cleanup stack and a top level TRAP for this thread in the Thread Entry Function, if it is required; else this thread may crash or panic.

Even though Open C can be used with hybrid applications well , the user must exercise caution with some of the APIs like popen. This particular API takes an EXE name as the argument. This EXE should be the one that is built using linking with libcrt0.lib. Otherwise the behavior of popen( ) will differ.

An application can have both C++ and C source codes. In this case, in order to give C linkage to C++ code- that is calling a function defined in a C++ file that may have C++ code, use extern “C”. For example:

// File: sample.c
  void OtherFoo();
  
  void Foo() {
     OtherFoo();
  }

// File: useme.cpp
  
  extern “C” {
  void OtherFoo() {
     //Can use any C++ code here
     //Can use Symbian C++ code here
  }
  }

Give feedback of this section

 


Using Open C++ on Carbide

This section provides procedures for using Open C++ on Carbide.

 


Creating Hello World project

  1. File > New and then click the Project tab to open the New Project dialog box (Figure 1)

    Figure 1 New Project

  2. Click Next to open the New Symbian OS C++ Project dialog box. Select the type of project for example, Basic console application (EXE) (Figure 2)

    Figure 2 New Symbian OS C++ Project

  3. Click Next to open the New Basic console application (EXE).Change the default location to a public SDK mapped location with Project name for example, E:\mobileruntime\HelloWorld (Figure 3)

    Figure 3 New Basic console application (EXE)

  4. Click Next and then select the particular SDK check box for example, E_S60_5_0_WK_200818 (Figure 4)

    Figure 4 SDKs and Build Configurations

  5. Click Finish. Check the C/C++ Projects for the Hello world Project window (Figure 5)

    Figure 5 C/C++ projects

  6. Update Helloworld.mmp with the following details
    /*
    ============================================================================
     Name		: HelloWorld.mmp
     Author	  : 
     Copyright   : Your copyright notice
     Description : This is the project specification file for HelloWorld.
    ============================================================================
    */
    
    TARGET			HelloWorld.exe
    TARGETTYPE		exe
    UID			0 0xE8212C84
    USERINCLUDE		../inc
    SOURCEPATH		../data
    
    
    SOURCEPATH	  	../src
    SOURCE		  	HelloWorld.cpp
    
    SYSTEMINCLUDE   /epoc32/include
    SYSTEMINCLUDE   /epoc32/include/osextensions/stdapis
    
    SYSTEMINCLUDE   /epoc32/include/stdapis
    SYSTEMINCLUDE   /epoc32/include/osextensions/stdapis/sys
    
    SYSTEMINCLUDE   /epoc32/include/stdapis/sys
    SYSTEMINCLUDE   /epoc32/include/osextensions/stdapis/stlport
    
    SYSTEMINCLUDE    /epoc32/include/stdapis/stlport
    
    
    STATICLIBRARY	libcrt0.lib
    LIBRARY			libstdcpp.lib
    LIBRARY			libc.lib
    LIBRARY			libpthread.lib
    LIBRARY			euser.lib
    
    OPTION CW -wchar_t on
    MACRO  _WCHAR_T_DECLARED
    
    
  7. Modify HelloWorld.cpp with the following code
    /
    ============================================================================
     Name		: HelloWorld.cpp
     Author	  : 
     Copyright   : Your copyright notice
     Description : Exe source file
    ============================================================================
    */
    #include	<iostream.h>
    	#include<cstring>
    // This is a GCCE toolchain workaround needed when compiling with GCCE
    // and using main() entry point
    #ifdef __GCCE__
    #include <staticlibinit_gcce.h>
    #endif
    using namespace std;
    
    class myclass {
    public:
      void show(){cout<<"Hello World\n"; }
    };
    
    int main()
    {
      myclass obj;
      obj.show();
      cout<<"Press a character to exit!";
      int c = getchar();
      return 0;
    }
    

 


Deleting Hello World Project

  1. Select Hello World in the C/C++ Projects window. (Figure 6) Right click project, then click Delete to open the Confirm Project Delete dialog box (Figure 7)

    Figure 6 Deleting Project

    Figure 7 Confirm Project Delete

  2. Click Yes to delete the project

This will remove the HelloWorld Project from the current projects window. There is no re-import option available in Carbide so to get the mmp modifications, delete the project and import it again.

 


Importing Hello World project in Carbide C++

  1. File > Import to open the Import dialog box (Figure 8)

    Figure 8 Import

  2. Click Next to open the File Import Wizard and select the bld.inf file

    Figure 9 File Import Wizard

  3. Click Next and Select SDK and build configurations. Examples for SDK and build configurations are winscw, armv5, GCCE.(Figure 10)

    Figure 10 SDK and build configurations

  4. Click Next and choose the project file (Figure 11)

    Figure 11 MMP Selection

  5. Click Next to find project properties (Figure 12), click Finish.to find the Hello World project under C/C++ Projects window.

    .

    Figure 12 Project Properties

 


Building Hello World Project

  1. Select HelloWorld project in C/C++ Projects window
  2. Right click HelloWorld project, and click Build project (Figure 13). Find the Build Complete results with error listing in the console view (Figure 14)

    Figure 13 Build Project

    Figure 14 Console View

  3. Click the run button to run HelloWorld project

    Figure 15 Run Button

  4. Type HelloWorld on the Emulator (Figure 16) and press enter to find the final output (Figure 17)

    Figure 16 Hello World

    Figure 17 Final Output

 


Changing the build configuration

  1. In the C/C++ Projects window select HelloWorld project. Right click project, and then click Active Build Configuration. Select the configuration from the submenu

    Figure 18 Active Build Configuration

    Find the project in the selected configuration.


©Nokia 2008

Back to top


All rights reserved. This documentation can be used in the connection with this Product to help and support the user.