C++中用Boost::Python调用Python模块

本文详细描述了在Windows11上配置Qt6.2、Boost1.8.4和CMake3.25.2环境,如何将Boost编译为静态库,以及在CMake中使用静态库、Python编程和C++调用Python的步骤,同时涉及了运行时Python环境变量的设置。
摘要由CSDN通过智能技术生成

这个过程有挺多坑,记录一下。我这里的环境:

Windows 11

Qt 6.2

Boost 1.8.4

CMake 3.25.2

Visual Stutio 2019(主要用于C++编译)

1、下载并将Boost编译为静态库

b2.exe toolset=msvc-14.2 install --prefix=boost安装路径 link=static

参考:

C++ Boost库在windows下的安装与使用 - 知乎 (zhihu.com)

2、CMake中使用静态库

set(Boost_USE_STATIC_LIBS ON) add_compile_definitions(BOOST_PYTHON_STATIC_LIB)

参考:

Win10下CMakeList.txt配置使用Boost.python - 知乎 (zhihu.com)

3、Python程序

如下(foo.py):

def greeting(s):
    print("Get the param in Python: ", s, " \n")
    return "Hello, I come from Pyhon world."

4、CMake程序

cmake_minimum_required(VERSION 3.14)

project(PythonCaller LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_LIBS ON)
set(PYTHON_ROOT 编译boost时python的运行环境根路径)#例如:d:/dev/Anaconda3
add_compile_definitions(BOOST_PYTHON_STATIC_LIB)

set(PYTHON_INCLUDE ${PYTHON_ROOT}/include)
set(PYTHON_LIBRARIES ${PYTHON_ROOT}/libs/python38.lib)

add_definitions(-DBOOST_PYTHON_NO_ASSERT)

find_package(Boost REQUIRED COMPONENTS python system)

include_directories(${PYTHON_INCLUDE} ${Boost_INCLUDE_DIRS})

MESSAGE( STATUS "Boost_LIBRARIES = ${Boost_LIBRARIES}. ")
MESSAGE( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
MESSAGE( STATUS "Boost_LIBRARY_DIRS = ${Boost_LIBRARY_DIRS}.")
MESSAGE( STATUS "Boost_PYTHON_LIBRARY = ${Boost_PYTHON_LIBRARY}.")

add_executable(PythonCaller main.cpp)


target_link_libraries(PythonCaller ${Boost_PYTHON_LIBRARY} ${PYTHON_LIBRARIES})

5、C++程序

#include <boost/python.hpp> // Boost.Python
#include <iostream>
#include <string>

namespace py=boost::python;

int main(int argc, char* argv[])
{
    Py_Initialize(); // 初始化 Python 解释器

    try
    {
        //导入模块 hello(hello.py)
        py::object hello = boost::python::import("foo");

        //获取属性(函数、类等)
        py::object greeting = hello.attr("greeting");

        //执行Python函数
        py::object py_res = greeting("Hello, I come from C++ world.");

        //转换并返回结果(使用 boost::python::extract 转换)
        std::string res = py::extract<std::string>(py_res);
        std::cout << "Get the result from python: " << res << std::endl;
    }
    catch (const boost::python::error_already_set&)
    {
        PyErr_Print();
        return -1;
    }

    Py_Finalize(); // 终止 Python 解释器

    return 0;
}

要注意头文件包含顺序,把”#include <boost/python.hpp>“放在STL包含头文件之前,否则报错:

xxx\Boost\1_84_0\include\boost-1_84\boost/assert/source_location.hpp(102): error C2039: "_snprintf": 不是 "std" 的成员

参考:

'_snprintf': is not a member of 'std' · Issue #32 · boostorg/system · GitHub

6、运行时设置python环境变量

需要设置Python环境变量。在Windows中执行如下命令(Linux用export命令):

set PYTHONHOME=Python环境根目录

否则C++ Boost调用python报错:

init_fs_encoding:failed to get the Python codec of the file

原因是python的环境变量没有配置好。

set PYTHONHOME=Python环境根目录

参考:

【BUG】C++ Boost调用python报错:init_fs_encoding:failed to get the Python codec of the file_fatal python error: init_fs_encoding: failed to ge-CSDN博客

7、运行结果

Get the param in Python:  Hello, I come from C++ world.

Get the result from python: Hello, I come from Pyhon world.

  • 25
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值