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

python的C、c++扩展

python的C、c++扩展
http://blog.chinaunix.net/u3/110228/showart_2148725.html
python的强大不仅表现在其功能上,而且还表现在其扩展能力上。
使用C/C++很容易编写python的模块,扩展python的功能。
同时将性能要求比较高的代码使用C/C++编写,能更好的弥补
脚本语言执行速度慢的缺陷。
1. python的C语言扩展
1.1 TestCLib.c: 提供python的模块接口
#include "Python.h"
#include <stdlib.h>
#include <stdio.h>
long fac(long);
// ------------------------------------------------------
// Make C code usable in Python
// ------------------------------------------------------
PyObject*   TestCLib_fac(PyObject * self, PyObject *args)
{
    long num;
    if ( ! PyArg_ParseTuple(args,"l",&num)){
        return NULL;
    }
    return (PyObject*)Py_BuildValue("l",fac(num));
}
static PyMethodDef  TestCLibMethods[] = {
    {"fac",TestCLib_fac,METH_VARARGS},
    {NULL,NULL}
};
void initTestCLib()
{
    Py_InitModule("TestCLib",TestCLibMethods);
}
1.2 test.c: 具体的C语言实现
#include <stdlib.h>
#include <stdio.h>
// ------------------------------------------------------
// Pure C code
// ------------------------------------------------------
long fac(long n)
{
    if ( n < 0){
        return fac(-n);
    } else if ( n < 2 ){
        return 1;
    } else {
        return n * fac(n-1);
    }
}
1.3 test.py: 测试脚本
#!/usr/bin/env python
import TestCLib as TestLib
for i in range(10,20) :
    f1 = TestLib.fac(i)
    print "%d! = %d"%(i,f1)
1.4 编译与运行
编译命令:
gcc -fPIC -shared  -o TestCLib.so TestCLib.c test.c -I /usr/local/python/include/python2.6/
将生成的动态链接库 T


相关文档:

C/C++编程必备

Windows C 程序设计入门与提高
http://download.chinaitlab.com/program/files/13246.html
单片机C语言入门
http://download.chinaitlab.com/program/files/12907.html
C++ 入门基础教程
http://download.chinaitlab.com/program/files/7617.html
C语言常用算法源代码
http://download.chinaitlab.com/program/files ......

数字表达式求值程序 (c/c++)

一个控制台下的数字表达式求值程序 (c/c++)
源代码见下:
#include <stdio.h>
#include <string>
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <stack>
using namespace std;
//设置运算符优先级的算法
int Priority(const string opera) // 运算符 ......

转]C,C++经典问题,及面试笔试题


1       编程基础
1.1    基本概念
1.         的理解:const char*, char const*, char*const的区别问题几乎是C++面试中每次    都会有的题目。 事实上这个概念谁都有只是三种声明方式非常相似很容易记混。 Bja ......

C/C++代码检视要点


版权申明:以下内容属于作者正在写作的《软件测试实践》一书的内容,未经许可不得用于任何正式出版物中,如果转载请注明出处。
作者:周伟明
代码检视要点
代码检视技能属于开发人员的基本功,能够很大程度地反应出开发人员的能力水平,前面4.4.1节已经讲过提高评审检视的方法。下面以实际的C/C++语言方面的代码来讲解 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号