如何在S60项目中调用标准C/C++的库函数
2010-03-30 13:49
337 查看
标题有点长,但还是能看明白的吧。
最近做一个S60项目,需要自己设计列表控件,滚动条和分页都要从头开始模拟,比较头疼。
在做分页时不免需要用到ceil()函数,而Symbian的Class Math没有提供ceil()函数,于是只能借助标准C。
借此机会说明一下如何在S60项目中使用标准C的库函数。
1.SDK文档中查知ceil:
Location: libc/math.h
ceil()
IMPORT_C double ceil(double);
2.打开工程的mmp文件,在SYSTEMINCLUDE 项后面添加/epoc32/include/libc,保存mmp
3.调用ceil的cpp文件中 #include <libc/math.h>
(#include <math.h>也是可以的)
4.完成如下调用:
double a = ceil(50.0 / 24); //a==3.0000000
题外话:
虽然Math类没有提供Ceil()函数,但是我们可以利用Math::DivMod64 / Math::UDivMod64函数进行模拟。
实现如下:
TInt64 mod; //用来保存余数
TInt a = Math::DivMod64(50,24,mod); // a== 2 , mod==2
TInt ceilValue = mod > 0? a + 1 : a; //ceilValue==3
STL for Symbian C++
http://whyandroid.com/symbian/66-stl-for-symbian-c.html
- Download OpenC/C++ Plugin
- Install it
- Modify the mmp file
That's all to use STL for Symbian C++
(The section below is copied from the help file of Nokia (The Standard C++ API Reference))
Changes to the MMP file
Add needed libraries used by the MMP file structure:
If developers want to use any of the Standard C++ library, they need to link to the corresponding library in the MMP file using the LIBRARY 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
The libcrt0.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 euser.lib
Add needed include paths
SYSTEMINCLUDE /epoc32/include/stdapis
SYSTEMINCLUDE /epoc32/include/stdapis/sys
SYSTEMINCLUDE /epoc32/include/stdapis/stlport
Linking of libstdcpp
The following snippet shows how to perform the linking to libstdcpp on an emulator:
#ifdef EPOC32
LIBRARY libstdcpp.lib
#else
FIRSTLIB ../udeb/libstdcpp.lib
STATICLIBRARY eexe.lib
#endif
Add the below option and macro in the MMP file
//This is required even if the wchar type is not used.
OPTION CW -wchar_t on
MACRO _WCHAR_T_DECLARED
NOTE: Standard C++ applications may require more stack space. The recommended stack size is 10K. To set the stack size to 10K add:
EPOCSTACKSIZE 0x10000
in the MMP file.
Example using main()
A simple example using main() as an entry point is described below. The example writes a text to a console.
Modify the MMP file as mentioned before.
Do usual C++ style coding.
// Include Files
#include <iostream>
#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;
}
最近做一个S60项目,需要自己设计列表控件,滚动条和分页都要从头开始模拟,比较头疼。
在做分页时不免需要用到ceil()函数,而Symbian的Class Math没有提供ceil()函数,于是只能借助标准C。
借此机会说明一下如何在S60项目中使用标准C的库函数。
1.SDK文档中查知ceil:
Location: libc/math.h
ceil()
IMPORT_C double ceil(double);
2.打开工程的mmp文件,在SYSTEMINCLUDE 项后面添加/epoc32/include/libc,保存mmp
3.调用ceil的cpp文件中 #include <libc/math.h>
(#include <math.h>也是可以的)
4.完成如下调用:
double a = ceil(50.0 / 24); //a==3.0000000
题外话:
虽然Math类没有提供Ceil()函数,但是我们可以利用Math::DivMod64 / Math::UDivMod64函数进行模拟。
实现如下:
TInt64 mod; //用来保存余数
TInt a = Math::DivMod64(50,24,mod); // a== 2 , mod==2
TInt ceilValue = mod > 0? a + 1 : a; //ceilValue==3
STL for Symbian C++
http://whyandroid.com/symbian/66-stl-for-symbian-c.html
- Download OpenC/C++ Plugin
- Install it
- Modify the mmp file
That's all to use STL for Symbian C++
(The section below is copied from the help file of Nokia (The Standard C++ API Reference))
Changes to the MMP file
Add needed libraries used by the MMP file structure:
If developers want to use any of the Standard C++ library, they need to link to the corresponding library in the MMP file using the LIBRARY 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
The libcrt0.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 euser.lib
Add needed include paths
SYSTEMINCLUDE /epoc32/include/stdapis
SYSTEMINCLUDE /epoc32/include/stdapis/sys
SYSTEMINCLUDE /epoc32/include/stdapis/stlport
Linking of libstdcpp
The following snippet shows how to perform the linking to libstdcpp on an emulator:
#ifdef EPOC32
LIBRARY libstdcpp.lib
#else
FIRSTLIB ../udeb/libstdcpp.lib
STATICLIBRARY eexe.lib
#endif
Add the below option and macro in the MMP file
//This is required even if the wchar type is not used.
OPTION CW -wchar_t on
MACRO _WCHAR_T_DECLARED
NOTE: Standard C++ applications may require more stack space. The recommended stack size is 10K. To set the stack size to 10K add:
EPOCSTACKSIZE 0x10000
in the MMP file.
Example using main()
A simple example using main() as an entry point is described below. The example writes a text to a console.
Modify the MMP file as mentioned before.
Do usual C++ style coding.
// Include Files
#include <iostream>
#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;
}
相关文章推荐
- 如何在qmake项目中在QML语言中调用C++代码
- 综合项目之闪讯破解(五)之 如何用C#调用C++编写的Dll
- 【Android学习笔记】如何使用Eclipse在Android项目中调用C/C++代码,及遇到的错误解决方法
- XCode里创建的Cocos2d-x项目如何C++与ObjC混编,以及调用APPLE API和ObjC函数
- 综合项目之闪讯破解(四)之 如何用C++编写可被C#调用的Dll
- C中如何调用C++?C++中如何调用C
- c++中如何在主函数中调用其他文件内的函数?
- C++成员函数如何调用线程
- c++函数如何返回多值,返回数组的函数该如何调用
- Java如何调用C++ DLL库
- C++如何调用C#编写的 DLL
- c++中如何解决函数调用二维数组的问题。
- 关于在C/C++语言中,函数如何返回数组,数组如何作为参数传递以及返回数组的函数该如何调用问题的总结
- C语言程序调用C++库函数方法
- 如何在C/C++语言中调用 sqlite 的函数
- 基于 wke 的浏览器:如何实现 js 和 c++ 的互相调用
- 如何编译由多个c++源文件组成的项目
- c/c++项目如何管理头文件
- C++ 如何调用系统时间
- [C++]VS跨项目调用全局函数