1. 程式人生 > >python與c++互動學習入門之5

python與c++互動學習入門之5

這次講講,如何擴充套件c++庫。通過boost.python把c++庫編譯成python能夠呼叫的dll。


通過上一次的教程後,大家都應該會使用boost.python了。把c++程式編譯成pyd檔案。由於c++有很多特性,所以,如果你的程

序用了很多的c++特性的話,那麼你必須做很多工作了。像虛擬函式,函式過載,繼承,預設值等等。具體如何轉化,請參

boost.python的文件了。


這幾天嘗試著把c++程式庫編譯成python可呼叫的dll,不知道為什麼一直不可用。。很是鬱悶。老是顯示如下的錯誤:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    import pydll
ImportError: No module named pydll

意思是說找不到dll。我把dll都copy到python/dlls下了還是不行,而且我確定python的sys.path包含了python/dlls目錄了。

很是不解。網上也很難找到資料,google了很長時間找不到有用的資料,好像中文方面的資料很少的。今天嘗試了一下google

英文資料,終於有了新的發現:
http://mail.python.org/pipermail/c++-sig/2007-February/011971.html
You are using Python2.5. In this version of Python you have to have
file extension
to be "pyd" - sge.pyd

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/

有人碰到的問題跟我的是一樣的。後面那個Roman回答了一下,是副檔名的問題!!!為什麼不支援dll呢?不解。回去試

了一下把字尾名改了就成功了。。。why???


下面來看一下我的那個簡單的例子:
這個例子來自於網上,
http://www.vckbase.com/document/viewdoc/?id=1540
C++ 擴充套件和嵌入 Python
作者:胡金山
原始碼下載地址:http://www.vckbase.com/code/downcode.asp?id=2777


這是一個非常簡單的dll工程。給python提供了一個函式static PyObject* Recognise(PyObject *self, PyObject *args)。


1、不使用boost.python庫來直接構建dll
接下來,我們來用C++為Python編寫擴充套件模組(動態連結庫),並在Python程式中呼叫C++開發的擴充套件功能函式。生成一個取名為

pyUtil的Win32 DLL工程,除了pyUtil.cpp檔案以外,從工程中移除所有其它檔案,並填入如下的程式碼:

// pyUtil.cpp
#ifdef PYUTIL_EXPORTS
#define PYUTIL_API __declspec(dllexport)
#else
#define PYUTIL_API __declspec(dllimport)
#endif

#include<windows.h>
#include<string>
#include<Python.h>
BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
        )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
std::string Recognise_Img(const std::string url)
{
    //返回結果
    return "從dll中返回的資料... : " +url;
}
static PyObject* Recognise(PyObject *self, PyObject *args)
{
    const char *url;
    std::string sts;
    if (!PyArg_ParseTuple(args, "s", &url))
        return NULL;
    sts = Recognise_Img(url);
    return Py_BuildValue("s", sts.c_str() );
}
static PyMethodDef AllMyMethods[] = {
    {"Recognise",  Recognise, METH_VARARGS},//暴露給Python的函式
    {NULL,      NULL}        /* Sentinel */
};
extern "C" PYUTIL_API void initpyUtil()
{
    PyObject *m, *d;
    m = Py_InitModule("pyUtil", AllMyMethods); //初始化本模組,並暴露函式
    d = PyModule_GetDict(m);
}

在Python程式碼中呼叫這個動態連結庫: (記得把dll的副檔名改為.pyd,另外dll的路徑要能夠被檢索到)
import pyUtil
result = pyUtil.Recognise("input url of specific data")
print "the result is: "+ result

 

2、使用boost.python庫來構建dll
用C++為Python寫擴充套件時,如果您願意使用Boost.Python庫的話,開發過程會變得更開心J,要編寫一個與上述pyUtil同樣功能

的動態連結庫,只需把檔案內容替換為下面的程式碼。當然,編譯需要boost_python.lib支援,執行需要boost_python.dll支援

。 
#include<string>
#include <boost/python.hpp>
using namespace boost::python;
#pragma comment(lib, "boost_python.lib")
std::string strtmp;
char const* Recognise(const char* url)
{
    strtmp ="從dll中返回的資料... : ";
    strtmp+=url;
    return strtmp.c_str();
}
BOOST_PYTHON_MODULE(pyUtil)
{
    def("Recognise", Recognise);
}

可以非常明顯地看到,用了boost.python庫之後,簡單了很多。因為boost.python為你做了很多的事情。。恩。

 

好像沒有講很多有用的東西,光顧著講了,呵呵。。。我也還在繼續學習之中。下次寫點什麼呢?繼續學習了哦