Introduction to Open C |
|
|
|
|
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
EPOCSTACKSIZE 0x10000in the MMP file.
|
|
A simple example using E32Main() as an entry point is described below. The example writes a text to a file.
#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
}
|
|
A simple example using main() as an entry point is described below. The example writes a text to a file.
#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;
}|
|
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
}
}
|
|
This section provides procedures for using Open C++ on Carbide.
|
|
![]() |
Figure 1 New Project
![]() |
Figure 2 New Symbian OS C++ Project
![]() |
Figure 3 New Basic console application (EXE)
![]() |
Figure 4 SDKs and Build Configurations
![]() |
Figure 5 C/C++ projects
/* ============================================================================ 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
/
============================================================================
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;
}
|
|
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.
|
|
![]() |
Figure 8 Import
![]() |
Figure 9 File Import Wizard
![]() |
Figure 10 SDK and build configurations
![]() |
Figure 11 MMP Selection
![]() |
Figure 12 Project Properties
|
|
![]() |
Figure 13 Build Project
![]() |
Figure 14 Console View
![]() |
Figure 15 Run Button
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
| ©Nokia 2008 |
|