這次講講,如何擴(kuò)展c++庫。通過boost.python把c++庫編譯成python能夠調(diào)用的dll。
通過上一次的教程后,大家都應(yīng)該會(huì)使用boost.python了。把c++程序編譯成pyd文件。由于c++有很多特性,所以,如果你的程
序用了很多的c++特性的話,那么你必須做很多工作了。像虛擬函數(shù),函數(shù)重載,繼承,默認(rèn)值等等。具體如何轉(zhuǎn)化,請參
boost.python的文檔了。
這幾天嘗試著把c++程序庫編譯成python可調(diào)用的dll,不知道為什么一直不可用。。很是郁悶。老是顯示如下的錯(cuò)誤:
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目錄了。
很是不解。網(wǎng)上也很難找到資料,google了很長時(shí)間找不到有用的資料,好像中文方面的資料很少的。今天嘗試了一下google
英文資料,終于有了新的發(fā)現(xiàn):
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/
有人碰到的問題跟我的是一樣的。后面那個(gè)Roman回答了一下,是文件擴(kuò)展名的問題!!!為什么不支持dll呢?不解。回去試
了一下把后綴名改了就成功了。。。why???
下面來看一下我的那個(gè)簡單的例子:
這個(gè)例子來自于網(wǎng)上,
http://www.vckbase.com/document/viewdoc/?id=1540
C++ 擴(kuò)展和嵌入 Python
作者:胡金山
源碼下載地址:http://www.vckbase.com/code/downcode.asp?id=2777
這是一個(gè)非常簡單的dll工程。給python提供了一個(gè)函數(shù)static PyObject* Recognise(PyObject *self, PyObject *args)。
1、不使用boost.python庫來直接構(gòu)建dll
接下來,我們來用C++為Python編寫擴(kuò)展模塊(動(dòng)態(tài)鏈接庫),并在Python程序中調(diào)用C++開發(fā)的擴(kuò)展功能函數(shù)。生成一個(gè)取名為
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)
{
//返回結(jié)果
return "從dll中返回的數(shù)據(jù)... : " +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的函數(shù)
{NULL, NULL} /* Sentinel */
};
extern "C" PYUTIL_API void initpyUtil()
{
PyObject *m, *d;
m = Py_InitModule("pyUtil", AllMyMethods); //初始化本模塊,并暴露函數(shù)
d = PyModule_GetDict(m);
}
在Python代碼中調(diào)用這個(gè)動(dòng)態(tài)鏈接庫: (記得把dll的擴(kuò)展名改為.pyd,另外dll的路徑要能夠被檢索到)
import pyUtil
result = pyUtil.Recognise("input url of specific data")
print "the result is: "+ result
2、使用boost.python庫來構(gòu)建dll
用C++為Python寫擴(kuò)展時(shí),如果您愿意使用Boost.Python庫的話,開發(fā)過程會(huì)變得更開心J,要編寫一個(gè)與上述pyUtil同樣功能
的動(dòng)態(tài)鏈接庫,只需把文件內(nèi)容替換為下面的代碼。當(dāng)然,編譯需要boost_python.lib支持,運(yùn)行需要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中返回的數(shù)據(jù)... : ";
strtmp+=url;
return strtmp.c_str();
}
BOOST_PYTHON_MODULE(pyUtil)
{
def("Recognise", Recognise);
}
可以非常明顯地看到,用了boost.python庫之后,簡單了很多。因?yàn)閎oost.python為你做了很多的事情。。恩。
好像沒有講很多有用的東西,光顧著講了,呵呵。。。我也還在繼續(xù)學(xué)習(xí)之中。下次寫點(diǎn)什么呢?繼續(xù)學(xué)習(xí)了哦