2021-01-29
- 作者:李长圣
- 邮箱:sheng0619@163.com
- 功能:演示在
qtcreator中使用boost.python库,实现c++ 和 python混合编程。 - 支持
window10系统
- 第一组 入门
-
ex1_1_cpp2lib扩展python,qtcreator中将使用boost.python库的cpp编译成lib,在python中调用该lib 。 进入编译目录.\build-ex1_1-Desktop_Qt_5_12_9_MSVC2017_64bit-Release\release打开cmd输入python test.py,看到输出hello,world说明成功!
-
ex1_2_cppEmbeddingPythonInterpreter将python作为cpp程序的脚本,实际未使用boost::python。 打开cmd进入build-ex1_2-Desktop_Qt_5_12_9_MSVC2017_64bit-Release\release, 同时将script.py复制到该目录,执行ex1_2.exe,输出#! /usr/bin/env python # Copyright Stefan Seefeld 2006. 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) print('Hello World !') number = 42 Hello World ! it is over ....说明成功!
-
ex1_3_QtEmbeddingPythonInterpreter与ex2类似,将python作为Qt GUI程序的脚本,实际未使用boost::python。输出#! /usr/bin/env python # Copyright Stefan Seefeld 2006. 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) print('Hello World !') number = 42 Hello World ! it is over ....说明运行成功!
-
ex1_4_embedding使用boost.python嵌入python解释器到c++应用。 输出I am called from cpp sys.version: 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] In cpp, method 1, five_squared: 25 In cpp, method 2, five_squared: 25 catch ZeroDivisionError in cpp In python, going to sleep for 10 seconds Traceback (most recent call last): File "<string>", line 1, in <module> ZeroDivisionError: division by zero说明运行成功!
-
ex1_5_execfiledebug模式 exec_file失败,不知道为什么。 输出hello.txt The five_squeared caculated by python is 25 3.9.1 (tags/v3.9.1:1e5d33e, Dec 7 2020, 17:08:21) [MSC v.1927 64 bit (AMD64)] Python has caculated foo as 2013说明运行成功!
-
- 第二组 编写单独的wrapper
ex2_1_MultFilesWrapper为c++源码,单独封装一个wrapper供python调用。进入exe目录,打开cmd,输入python .\test.py, 输出说明运行成功!15 15ex2_2_WrapperVector传递c++的std::vector<A>类型到python。 进入exe目录,打开cmd,输入python .\test.py, 输出说明运行成功!2 6
- 第三组 让python处理
c++中的对象数据,此时是传递了一个c++对象的指针给python,不需要生成pyd格式的库。传递c++中的对象实例给python,让python处理,之后c++可以使用该处理后的对象实例。ex3_1_cpp_object_to_python_single_file传递c++中的对象实例到python。输出说明运行成功!hello, I am Python hello from pythonex3_2_cpp_object_to_python_mult_file同ex3_1,区别是分离成多个文件。输出说明运行成功!default constructor hello init hello from python hello, I am Pythonex3_3_cpp_object_to_python_execfile传递c++对象实例到python,作相应计算后,返回c++。debug模式 exec_file失败,不知道为什么。输出说明运行成功!main begin main2 main3 In Python: values in c++ object are now: -1 and -2ex3_4_cpp_object_to_python_execfile传递c++对象实例作为全局变量传入python,作相应计算后,返回c++。输出说明运行成功!worldObjectPtr->greet(): Hello from Python!ex3_5_cpp_object_to_python_execfile_include_h与ex3_4比,独立出来一个头文件ex3_6_QtEmbeddingPython与ex3_5比,加入Qt窗口说明运行成功! 这里尤其需要注意!!!worldObjectPtr->greet(): Hello from Python!- 如果要使用动态链接库,应该将动态链接库复制到exe所在目录。注意动态库名为
boost_python39-vc141-mt-x64-1_75.dll和boost_python39-vc141-mt-x64-1_75.lib。 - 如果使用静态库,则不需要。静态库
libboost_python39-vc141-mt-x64-1_75.lib
- 如果要使用动态链接库,应该将动态链接库复制到exe所在目录。注意动态库名为
编译运行实例,使用 qtcreator 打开各实例文件夹下的 *.pro 运行。
注意:
- 均编译为lib静态库
boost_python39-vc141-mt-gd-x64-1_75.libDebug版本boost_python39-vc141-mt-x64-1_75.libRelease版本
- 编译后的libboost_python39-vc141-mt-gd-x64-1_75.lib,需要去掉lib前缀
- 可以不改库名,需要在cpp中加入以下宏
#define BOOST_ALL_NO_LIB //告诉boost禁用自动链接。
- 可以不改库名,需要在cpp中加入以下宏
- qtcreator中使用 boost.python 和 python3.lib 库时,Debug Relesse时要选择编译好的对应的版本
-
下载安装python,版本
python-3.9.1-amd64.exehttps://www.python.org/downloads/release/python-391/ 选择Windows installer (64-bit)- 可能需要添加环境变量到PATH
-
编译 boost.python
-
下载解压boost,版本
boost_1_75_0.ziphttps://dl.bintray.com/boostorg/release/1.75.0/source/ -
打开
VS 2017的开发人员命令提示符进入解压的目录,F:\MyProgram\QtProject\libs\boost_1_75_0,运行bootstrap.bat生成
b2.exe
注意 这里必须使用VS 2017的开发人员命令提示符,可能对应下面的architecture=x86 -
编译
b2 stage --toolset=msvc-14.1 architecture=x86 address-model=64 --with-python link=static runtime-link=shared threading=multi debug release完成后会生成一个
F:\MyProgram\QtProject\libs\boost_1_75_0\stage文件夹和其它文件夹。这里只需要stage文件夹,此是我们想要的。在这个文件加下的lib文件夹下生成了**.lib文件。
注意:编译完 boost.python,生成的静态库名字可能是libboost_python39-vc141-mt-gd-x64-1_75,需要改成boost_python39-vc141-mt-gd-x64-1_75
注意:如果runtime-link=static,生成静态库libboost_python39-vc141-mt-sgd-x64-1_75,无法生成hello.dll动态链接库
-
-
qt5 (qtcreator) 中使用该库
- http://download.qt.io/archive/qt 下载安装Qt,本实例使用的版本
Qt 5.12.9 - qtcreat中,使用boost.python时
inclue为F:\MyProgram\QtProject\libs\boost_1_75_0,其中的boost即为头文件。lib在F:\MyProgram\QtProject\libs\boost_1_75_0\stage\lib
- 注意: 为了让qtcreator中能找到对应的库,这里需要修改库名,去掉前缀
lib。libboost_python39-vc141-mt-gd-x64-1_75.lib改为boost_python39-vc141-mt-gd-x64-1_75.liblibboost_python39-vc141-mt-x64-1_75.lib改为boost_python39-vc141-mt-x64-1_75.lib
不然将会找不到相应的库,qtcreator提示错误LINK : fatal error LNK1181: 无法打开输入文件“F:\MyProgram\QtProject\libs\boost_1_75_0\stage\lib\boost_python39-vc141-mt-x64-1_75.lib”
- qtcreator中使用boost.python,编译错误原因
-
boost.python 和 python3.lib 的 Debug Relesse版本要对应,例如
- boost_python39-vc141-mt-gd-x64-1_75.lib 为debug版本。
- boost_python39-vc141-mt-x64-1_75.lib 为release版本。
Qtcreator中Debug模式编译程序时,*.pro中要使用debug版本库
win32:LIBS += $$quote(F:\MyProgram\QtProject\libs\boost\vc141-x64\lib\boost_python39-vc141-mt-gd-x64-1_75.lib)否则可能弹出以下错误,无法打开文件
LINK : fatal error LNK1104: 无法打开文件“F:\MyProgram\QtProject\libs\boost\vc141-x64\lib\boost_python39-vc141-mt-gd-x64-1_75.lib”
-
- http://download.qt.io/archive/qt 下载安装Qt,本实例使用的版本
-
库名解释 libboost_regex-vc71-mt-sgd-x86-1_34.lib
libOn Windows, only ordinary static libraries use the lib prefixvc71使用的编译器mtmultithreading support enabled- sgd
- s runtime-link=static 有该值不能用来生成hello.dll动态链接库
- g using debug versions of the standard and runtime support libraries. runtime-debugging=on
- d variant=debug模式,qtcreator中debug模式选用有该标示的库
环境:windows10+vs2017+boost1_67 64位
下载地址:https://dl.bintray.com/boostorg/release/1.67.0/binaries/ 找到boost_1_67_0-msvc-14.1-64.exe并下载
执行boost_1_67_0-msvc-14.1-64.exe,选择安装目录进行安装
注意 安装目录F:\MyProgram\QtProject\libs\boost\boost_1_75_0\lib64-msvc-14.1下有编译好的所有库
进入boost安装目录,执行bootstrap.bat,稍等几分钟,会生成bjam.exe文件。 然后打开vs2017 64位命令行,在开始菜单中的vs2017下。进入boost目录,执行下面命令:
bjam stage --toolset=msvc-14.1 architecture=x86 address-model=64 --without-graph --without-graph_parallel --stagedir="D:\local\boost_1_67_0\bin\vc141-x64" link=static runtime-link=shared runtime-link=static threading=multi debug release
等待很长的时间之后。
- 右键项目 》属性 》C/C++ 》Addtional Include Directories 》添加boost目录
- 右键项目 》属性 》Linker 》General 》Addtional Library Directories 》添加boost下编译好的lib目录
- 右键项目 》属性 》Linker 》Input 》Addtional Dependencies 》添加依赖的库名称