/QtBoostPythonHelloWorld

c++和python混合编程实例,qtcreator中使用boost.python。

Primary LanguageQMakeMIT LicenseMIT

BoostPythonHelloWorld

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_execfile debug模式 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
       15
      
      说明运行成功!
    • ex2_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 python
      
      说明运行成功!
    • ex3_2_cpp_object_to_python_mult_file 同ex3_1,区别是分离成多个文件。输出
       default constructor
       hello init
       hello from python
       hello, I am Python
      
      说明运行成功!
    • ex3_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 -2
      
      说明运行成功!
    • ex3_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.dllboost_python39-vc141-mt-x64-1_75.lib
      • 如果使用静态库,则不需要。静态库libboost_python39-vc141-mt-x64-1_75.lib

编译运行实例,使用 qtcreator 打开各实例文件夹下的 *.pro 运行。

vs2017 win10 编译boost.python,qt5 (qtcreator) 中使用该库

注意:

  1. 均编译为lib静态库
    • boost_python39-vc141-mt-gd-x64-1_75.lib Debug版本
    • boost_python39-vc141-mt-x64-1_75.lib Release版本
  2. 编译后的libboost_python39-vc141-mt-gd-x64-1_75.lib,需要去掉lib前缀
    • 可以不改库名,需要在cpp中加入以下宏
       #define BOOST_ALL_NO_LIB
       //告诉boost禁用自动链接。
      
  3. qtcreator中使用 boost.python 和 python3.lib 库时,Debug Relesse时要选择编译好的对应的版本
  • 下载安装python,版本 python-3.9.1-amd64.exe https://www.python.org/downloads/release/python-391/ 选择 Windows installer (64-bit)

    • 可能需要添加环境变量到PATH
  • 编译 boost.python

    1. 下载解压boost,版本 boost_1_75_0.zip https://dl.bintray.com/boostorg/release/1.75.0/source/

    2. 打开 VS 2017的开发人员命令提示符进入解压的目录,F:\MyProgram\QtProject\libs\boost_1_75_0,运行

      bootstrap.bat 
      

      生成 b2.exe
      注意 这里必须使用 VS 2017的开发人员命令提示符,可能对应下面的architecture=x86

    3. 编译

      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时
      • inclueF:\MyProgram\QtProject\libs\boost_1_75_0,其中的boost即为头文件。
      • libF:\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.lib
      • libboost_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”
        
  • 库名解释 libboost_regex-vc71-mt-sgd-x86-1_34.lib

    • lib On Windows, only ordinary static libraries use the lib prefix
    • vc71 使用的编译器
    • mt multithreading 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模式选用有该标示的库

参考 boost学习笔记(windows+vs2017+boost1.67 64位编译)

环境:windows10+vs2017+boost1_67 64位

boost下载

下载地址: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  

等待很长的时间之后。

配置vs

  • 右键项目 》属性 》C/C++ 》Addtional Include Directories 》添加boost目录
  • 右键项目 》属性 》Linker 》General 》Addtional Library Directories 》添加boost下编译好的lib目录
  • 右键项目 》属性 》Linker 》Input 》Addtional Dependencies 》添加依赖的库名称