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 some linker errors. We also need to link with 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. So, the developer does not have to worry about them. 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 we are not going to write E32Main within our 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 

 


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, we have to 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 , we have to be a bit careful 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.

We can also have an application that has both C++ and C source codes. In this case if we want to give C linkage to C++ code- that is calling a function defined in a C++ file that may have C++ code, we can 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


©Nokia 2007

Back to top


This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any or all of this material requires the prior written consent of Nokia. This material also contains confidential information, which may not be disclosed to others without the prior written consent of Nokia.

Nokia is a registered trademark of Nokia Corporation. S60 and logo is a trademark of Nokia Corporation. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. Other company and product names mentioned herein may be trademarks or tradenames of their respective owners.