使用boost.python在python调用C++接口(windows)
来源:互联网 发布:用友软件上海分公司 编辑:程序博客网 时间:2024/04/24 20:11
今日需要用到python中直接调用C++功能,按照网上的例程配置boost.python的环境,总是不能成功,最后汇总了几个文件,终于尝试成功,记录于此以备查阅。
boost.python可以用来封装C++(C不适用)的代码,形成可以被python调用的.pyd库。
具体的流程如下所示:(boost.python 1.42 + python 2.7 + vs 2005)
1)下载boost.python并解压到C:\boost_1_42_0(http://www.boost.org/)
2)执行c:\boost_1_42_0\bootstrap.bat,运行之后会build出一个bjam.exe文件,这个就是编译引导工具。编译时可以指定需要编译的版本(比如VC8,VC9等),然后使用VS 2005命令提示行(Tools\Visual Studio 2005 Command Prompt)定位到\boost_1_42_0下(不要使用cmd),设置一下环境变量:
set PYTHON_ROOT=C:\Python27(python的安装路径)
set PYTHON_VERSION=2.7(python的版本号)
然后运行bjam -sTools=vc-8_0,就开始编译过程,时间比较长。
如果没有生成boost_python-vc80-mt-gd-1_42.lib,则运行bjam --without-python --toolset=msvc-8.0 --build-type=complete --prefix="c:\boost_1_42_0" stage进行完全编译,以生成c++编译所需的lib库。
3)在vs Tools\Options 修改文件目录:
将c:\python27\include, c:\boost_1_42_0加入Include files
将c:\python27\libs, c:\boost_1_42_0\libs, c:\boost_1_42_0\stage\lib 加入Library files
4)新建一个工程example, Win32 COnsole Application, DLL, Empty Project.
加入example.cpp文件,文件内容如下:
// Copyright Ralf W. Grosse-Kunstleve 2002-2004. Distributed under the Boost
// Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <string>
namespace { // Avoid cluttering the global namespace.
// A couple of simple C++ functions that we want to expose to Python.
std::string greet() { return "hello, world"; }
int square(int number) { return number * number; }
}
namespace python = boost::python;
// Python requires an exported function called init<module-name> in every
// extension module. This is where we build the module contents.
BOOST_PYTHON_MODULE(getting_started1)
{
// Add regular functions to the module.
python::def("greet", greet);
python::def("square", square);
}
5)编译工具,将生成的.dll文件重命名为getting_started1.pyd.
6)cmd进入.pyd所在目录,进行python测试,看是否可以正常引入。
- 使用boost.python在python调用C++接口(windows)
- Python调用C/C++的接口(Boost)
- python调用c接口
- Linux使用Python调用C/C++接口
- Python调用采用Boost Python封装的c++(2)
- Python调用采用Boost Python封装的c++(2)
- python 使用Boost.python 调用c++
- boost.python,在python中调用c++接口注意的问题(python2.5.4)
- 在Python中调用C++,使用SWIG
- python调用C和C++代码(BOOST)
- Python和C|C++的混编(一):Python调用C、C++---Boost库
- 使用boost实现python调用c++
- Python 使用库ctypes调用C代码接口
- Boost Python 实现C调用python错误解决方法
- Python调用C/C++(使用SWIG)
- c++调用Python [boost::python]
- Boost.Python C++与Python的互相调用之Python调用c/c++函数
- Python接口(1):使用Python调用C/C++的种种方法
- CSS中zoom:1的作用 ,小标签大作用
- UI 基础控件(一)-----------UILabel
- 导入工程的“The importandroid.os cannot be resolved”错误解决办法
- android 显示gif图片
- 如何简化从hbase中查询数据
- 使用boost.python在python调用C++接口(windows)
- linux ------ 闲聊input设备
- windows与office
- NS2中的数据包common头结构hdr_cmn
- unity3d 制作动态天空盒——实现时时环境反射
- BZOJ3170[TJOI2013]松鼠聚会
- 修改ubuntu下Eclipse默认的黑色注释
- 程序员必知的8大排序(java实现)(一)(插入+希尔+简单选择)
- iOS开发xcode报错:"xxxxxx"has been modified since the precompiled header was built