python绑定c++程序
很多时候需要给c++程序提供一种使用上的灵活性,脚本语言在这里就变得很重要了。采用Boost.Python为c++程序加一层shell,比较简单、简洁,对原有的c++代码也没有侵入性。今天试了一下,感觉不错,可以把它集成在现在正在做的项目中。
我主要参照David Abrahams的"Building Hybrid Systems with Boost.Python"(http://www.boost-consulting.com/writing/bpl.html)一文,该文中对编译 过程说的较少,偶就略做补充,为新手节省点时间(偶也是python新手)。
为c++类加python shell过程基本上如下:
(1)为c++类编写一个Boost.Python wrapper
(2)编译成so
(3)可以在python中调用了
针对David Abrahams的例子,偶的源文件如下:
例1:hello world 函数
(1)hello.cpp
(1)hello_class.cpp
更复杂的调用见上面提到的David Abrahams的文章。
我主要参照David Abrahams的"Building Hybrid Systems with Boost.Python"(http://www.boost-consulting.com/writing/bpl.html)一文,该文中对编译 过程说的较少,偶就略做补充,为新手节省点时间(偶也是python新手)。
为c++类加python shell过程基本上如下:
(1)为c++类编写一个Boost.Python wrapper
(2)编译成so
(3)可以在python中调用了
针对David Abrahams的例子,偶的源文件如下:
例1:hello world 函数
(1)hello.cpp
#include <stdexcept>
char const* greet(unsigned x)
{
static char const* const msgs[] = { "hello", "Boost.Python", "world!" };
if (x > 2)
throw std::range_error("greet: index out of range");
return msgs[x];
}
(2)hello_wrap.cppchar const* greet(unsigned x)
{
static char const* const msgs[] = { "hello", "Boost.Python", "world!" };
if (x > 2)
throw std::range_error("greet: index out of range");
return msgs[x];
}
#include <boost/python.hpp>
using namespace boost::python;
char const* greet(unsigned x);
BOOST_PYTHON_MODULE(hello)
{
def("greet", greet, "return one of 3 parts of a greeting");
}
(3)makefileusing namespace boost::python;
char const* greet(unsigned x);
BOOST_PYTHON_MODULE(hello)
{
def("greet", greet, "return one of 3 parts of a greeting");
}
PYTHON_INCLUDE_FLAGS = \
-I/usr/include/python2.4
LIB_FLAGS = \
-lboost_python
SOURCE = \
hello.cpp hello_wrap.cpp
all:${SOURCE}
g++ ${PYTHON_INCLUDE_FLAGS} ${SOURCE} ${LIB_FLAGS} -shared -o hello.so
clean:
rm -f hello *.o *.out *.so
(4)hello.py-I/usr/include/python2.4
LIB_FLAGS = \
-lboost_python
SOURCE = \
hello.cpp hello_wrap.cpp
all:${SOURCE}
g++ ${PYTHON_INCLUDE_FLAGS} ${SOURCE} ${LIB_FLAGS} -shared -o hello.so
clean:
rm -f hello *.o *.out *.so
import hello
for x in range(3):
print hello.greet(x)
例2:hello world类for x in range(3):
print hello.greet(x)
(1)hello_class.cpp
#include <boost/python.hpp>
#include <iostream>
using namespace std;
using namespace boost::python;
class World
{
public:
void set(std::string msg) { this->msg = msg; }
void greet()
{
cout << this->msg << endl;
}
string msg;
};
BOOST_PYTHON_MODULE(hello)
{
class_<World> w("World");
w.def("greet", &World::greet);
w.def("set", &World::set);
};
(2)makefile#include <iostream>
using namespace std;
using namespace boost::python;
class World
{
public:
void set(std::string msg) { this->msg = msg; }
void greet()
{
cout << this->msg << endl;
}
string msg;
};
BOOST_PYTHON_MODULE(hello)
{
class_<World> w("World");
w.def("greet", &World::greet);
w.def("set", &World::set);
};
PYTHON_INCLUDE_FLAGS = \
-I/usr/include/python2.4
LIB_FLAGS = \
-lboost_python
SOURCE = \
hello_class.cpp
all:${SOURCE}
g++ ${PYTHON_INCLUDE_FLAGS} ${SOURCE} ${LIB_FLAGS} -shared -o hello.so
clean:
rm -f hello *.o *.out *.so
(3)hello_class.py-I/usr/include/python2.4
LIB_FLAGS = \
-lboost_python
SOURCE = \
hello_class.cpp
all:${SOURCE}
g++ ${PYTHON_INCLUDE_FLAGS} ${SOURCE} ${LIB_FLAGS} -shared -o hello.so
clean:
rm -f hello *.o *.out *.so
import hello
planet = hello.World()
planet.set('howdy')
planet.greet()
planet = hello.World()
planet.set('howdy')
planet.greet()
更复杂的调用见上面提到的David Abrahams的文章。
版权所有,欢迎转载
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?