來自: http://www.itivy.com/python/archive/2012/2/13/python-call-cpp-module.htmlhtml
Python能夠調用C++寫的擴展模塊,具體調用的方法以下:python
1.去http://www.boost.org/下載boost包,我用的是boost_1_48_0.zipbootstrap
2.解壓,進入目錄找到bootstrap.bat 腳本,並執行,會在相同文件夾生成 bjam.exe文件ui
3.打開VS2005 在tools->...command prompt,定位到boost主目錄下,執行bjam --with-python --build-type=complete stagespa
編譯成功後會在 ...\boost_1_48_0\stage\lib 下找到python所使用的boost庫包括.lib 和 .dll.net
4.在vs2005中新建一個空的動態連接庫工程,設置工程的屬性,把boost的庫路徑 ...\boost_1_48_0\stage\libcode
python庫路徑 ...\Python27\libs (python安裝目錄)加上;把頭文件路徑 ...\Python27\include 和 ...\boost_1_48_0 加上htm
5.新建hello.cpp文件,把下面代碼拷進去blog
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
char
const
* greet()
{
return
"hello, world"
;
}
BOOST_PYTHON_MODULE(hello_ext)
{
using
namespace
boost::python;
def(
"greet"
, greet);
}
|
也能夠在工程屬性中設置輸出名爲hello_ext.pyd
注意:必須與BOOST_PYTHON_MODULE(hello_ext)中模塊名相同(我用的是hello_ext)ip
7.最後把hello_ext.pyd和boost_python-vc90-mt-gd-1_48.dll
拷貝到python的工做目錄下,不太重新驗證一下好像boost_python-vc90-mt-gd-1_48.dll不拷也能夠
8.在python 工做目錄下新建hello.py編寫以下代碼:
import hello_ext
hello_ext.greet()
執行就能夠了。
原文連接:http://blog.csdn.net/co_diy/article/details/7250792