python通过boost.python使用c/c++

安装boost环境

https://www.boost.org/下载,解压设置环境变量即可在c/c++中日常使用
但是对于boost.python来说需要对boost进行编译

编写user-config.jam文件,设置python目录、链接文件目录、头文件目录,位于c:/users/用户/user-config.jam

1
2
3
4
5
6
7
8
import toolset : using ;
using python
: 3.7
: D:/Applications/python/python37/python.exe
: D:/Applications/python/python37/include
: D:/Applications/python/python37/libs
: <toolset>gcc
;
1
2
bootstrap.bat gcc  #生成b2.exe等文件
b2 --with-python --toolset=gcc architecture=x86 address-model=64 link=shared #在stage/lib下生成boost.python的对应Python版本的dll和dll.a文件

编写并编译c++程序

使用boost.python导出需要使用的c++模块,使用说明见https://wiki.python.org/moin/boost.python/module
需要注意的是,BOOST_PYTHON_MODULE(my_module)中的my_module必须和文件名相同
编译c++程序生成dll文件

python使用c++编译后的dll文件

将使用boost.python的dll文件改名为pyd(python的链接库文件)
在Python中添加路径(可以使用sys.path.append或者复制到python执行目录下) ,需要注意,除了添加.pyd文件之外,还需要添加 boost.python的对应Python版本的dll文件

1
import my_module