這次講講,如何擴展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為你做了很多的事情。。恩。
好像沒有講很多有用的東西,光顧著講了,呵呵。。。我也還在繼續學習之中。下次寫點什么呢?繼續學習了哦