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

 


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


Back to top


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