c++中嵌入python入门3

这次主要讲讲怎么把python中的class嵌入到c++中去。
顺便讲讲元组的操作和怎么编译python源代码。

1. 首先讲讲元组的操作
由于参数是通过元组传进去的,所以我们不能老是通过Py_BuildValue这个函数来操作元组,那样太不方便了。
Python提供了元组相关的操作,下面这个例子演示了如何操作。主要是下面几个函数:
//new一个元组,传入size
pArgs = PyTuple_New(argc - 3); 
//set元组的直,第一个为元组,第二个为index(从0开始),第三个为value
PyTuple_SetItem(pArgs,0,Py_BuildValue("i",2000) );
PyTuple_SetItem(pArgs,1,Py_BuildValue("i",8) );

来自python doc的一个例子

#include <Python.h>
int
main(int argc, char *argv[])
{
    PyObject *pName, *pModule, *pDict, *pFunc;
    PyObject *pArgs, *pValue;
    int i;

    if (argc < 3) {
        fprintf(stderr,"Usage: call pythonfile funcname [args]\n");
        return 1;
    }

    Py_Initialize();
    pName = PyString_FromString(argv[1]);
    /* Error checking of pName left out */

    pModule = PyImport_Import(pName);
    Py_DECREF(pName);

    if (pModule != NULL) {
        pFunc = PyObject_GetAttrString(pModule, argv[2]);
        /* pFunc is a new reference */

        if (pFunc && PyCallable_Check(pFunc)) {
            pArgs = PyTuple_New(argc - 3);
            for (i = 0; i < argc - 3; ++i) {
                pValue = PyInt_FromLong(atoi(argv[i + 3]));
                if (!pValue) {
                    Py_DECREF(pArgs);
                    Py_DECREF(pModule);
                    fprintf(stderr, "Cannot convert argument\n");
                    return 1;
                }
                /* pValue reference stolen here: */
                PyTuple_SetItem(pArgs, i, pValue);
            }
            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs);
            if (pValue != NULL) {
                printf("Result of call: %ld\n", PyInt_AsLong(pValue));
                Py_DECREF(pValue);
            }
            else {
                Py_DECREF(pFunc);
                Py_DECREF(pModule);
                PyErr_Print();
                fprintf(stderr,"Call failed\n");
                return 1;
            }
        }
        else {
            if (PyErr_Occurred())
                PyErr_Print();
            fprintf(stderr, "Cannot find function \"%s\"\n", argv[2]);
        }
        Py_XDECREF(pFunc);
        Py_DECREF(pModule);
    }
    else {
        PyErr_Print();
        fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
        return 1;
    }
    Py_Finalize();
    return 0;
}


2. class操作
把下面加入到test2.py中去。定义了一个很简单的类,有一个name成员变量,一个printName成员函数
class TestClass:
    def __init__(self,name):
        self.name = name

    def printName(self):
        print self.name

cpp文件
#include <python.h>
int main()
{
 Py_Initialize();

 PyObject * pModule = NULL;
 PyObject * pFunc   = NULL;
 PyObject * pArg    = NULL;
 PyObject * pClass  = NULL;
 PyObject * pObject = NULL;

 pModule = PyImport_ImportModule("test2");
 pClass  = PyObject_GetAttrString(pModule, "TestClass");//得到那个类
 pArg = PyTuple_New(1);
 PyTuple_SetItem(pArg, 0, Py_BuildValue("s", "Jacky"));
 pObject = PyEval_CallObject(pClass, pArg);//生成一个对象,或者叫作实例

 pFunc = PyObject_GetAttrString(pObject, "printName");//得到该实例的成员函数
 PyEval_CallObject(pFunc, NULL);//执行该实例的成员函数

 Py_Finalize();

 return 0;
}


没有什么资料,就先写到这里了。下面介绍一下怎么build python25的源代码

3. 编译python源代码
为什么要编译呢?因为没有python25_d.lib!呵呵。顺便可以了解一下代码结构。
解压缩后,有好多目录,其中pcbuild和pcbuild8是我们要的。pcbuild对应着vc7.1的,pcbuild8对应着vc8.0的
因为在用vc7.1,也就是2003了。所以我就说说怎么用2003来编译吧。事实上是从一位牛人那里学来的

http://blog.donews.com/lemur/archive/2005/12/17/660973.aspx,那位大哥大概一年半前就在解剖python了,厉害

阿。看来我只能后来居上了,娃哈哈。我按照他说的试了一下,编译成功!

不过遇到一点小问题,用vc2003打开那个solution的时候,发现作者没有把source code control去掉,郁闷!害的我

们打开的时候一堆messagebox。不过不用管它就好了,一直确定。最后试了一下那个python25_d.lib,没问题。不过记

得把python25_d.dll copy到一个能被找到的目录,比如说c:\windows\system32\下面。python25.dll也在这个目录下

面。over。恩。

猜你喜欢

转载自blog.csdn.net/shaozhulei555/article/details/44808083