自学内容网 自学内容网

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

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

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.


原文地址:https://blog.csdn.net/DLite/article/details/136045599

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!