易截截图软件、单文件、免安装、纯绿色、仅160KB

Python 的C语言扩展

操作系统:linux debian 4.0, python版本2.5
s1:安装python2.5-dev。因为Python.h是在dev包中才有。
test@debian:~/test_python_c$ aptitude search python2.5-dev
p python2.5-dev - Header files and a static library for Python.
test@debian:~/test_python_c$ sudo aptitude install python2.5-dev
...
test@debian:~/test_python_c$ aptitude search python2.5-dev
i python2.5-dev - Header files and a static library for Python.
s2:准备测试文件——C语言函数
//filename:example.c
int fact(int n)
{
if(n<=1)
return 1;
else
return n*fact(n-1);
}

s3: 编写封装接口
//filename: wrap.c
#include <Python.h>
PyObject* wrap_fact(PyObject* self, PyObject* args)
{
int n, result;
if (! PyArg_ParseTuple(args, "i:fact", &n))
return NULL;
result = fact(n);
return Py_BuildValue("i", result);
}
static PyMethodDef exampleMethods[] =
{
{"fact", wrap_fact, METH_VARARGS, "Caculate N!"},
{NULL, NULL}
};
void initexample()
{
PyObject* m;
m = Py_InitModule("example", exampleMethods);
}

s4:编译连接
gcc -fpic -c -I /usr/include/python2.5 -I /usr/lib/python2.5/config example.c wrap.c
gcc -shared -o example.so example.o wrap.o
test@debian:~/test_python_c$ ls
example.c example.o example.so wrap.c wrap.o
s5:测试example扩展模块
test@debian:~/test_python_c$ python
Python 2.5 (release25-maint, Jul 20 2008, 20:47:25)
[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import example
>>> dir(example)
['__doc__', '__file__', '__name__', 'fact']
>>> example.fact(5)
120
>>> example.fact(16)
2004189184
>>> example.fact(17)
-288522240

说明fact(17)的时候溢出了。
操作系统:windowsXP, python版本2.5
#TODO:


相关文档:

zz Emacs C

日期:2009-11-21   10:54:22
本节主要参考:
    曹乐的《在Emacs下用C/C++编程》
    王纯业的《Emacs 一个强大的平台》
    emacswiki.org
emcas难学易用,可扩展性强。有人把她当作信仰,有人认为他是魔鬼!学习首先记住基本的键盘快捷键,学会常用插件, ......

C/C++软件工程师就业求职手册节选二

 5、#define宏定义。宏只是简单的文本替换,很容易引起歧义。
#include <stdio.h>
#define CONS(a,b) (int)(a##e##b)
#define STR(s) #s
int main()
{
   printf(STR(vck));
   printf("\n");
   printf("%d\n",CONS(2,3));
   return 0; ......

C/C++——小编谈C语言函数那些事(12)

C程序是由一组或是变量或是函数的外部对象组成的。 函数是一个自我包含的完成一定相关功能的执行代码段。下面小编和大家分享下C语言中的函数。
1.       ldexp函数
ldexp函数的功能是计算value*2的幂,其用法为:double ldexp(double value, int exp);程序实例如下:
    ......

C/C++多种方法获取文件大小

#include <iostream>
#include <io.h>
#include <sys\stat.h>
#include <afx.h>
#define _AFXDLL
using namespace std;
void main()
{
    // 此文件在工程打开状态下为不可访问
    char* f ......

C可变参数函数 实现

 C函数要在程序中用到以下这些宏:
void va_start( va_list arg_ptr, prev_param );
type va_arg( va_list arg_ptr, type );
void va_end( va_list arg_ptr );
va_list:用来保存宏va_start、va_arg和va_end所需信息的一种类型。为了访问变长参数列表中的参数,必须声明
      & ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号