这个过程有挺多坑,记录一下。我这里的环境:
Windows 11
Qt 6.2
Boost 1.8.4
CMake 3.25.2
Visual Stutio 2019(主要用于C++编译)
b2.exe toolset=msvc-14.2 install --prefix=boost安装路径 link=static
参考:
C++ Boost库在windows下的安装与使用 - 知乎 (zhihu.com)
set(Boost_USE_STATIC_LIBS ON) add_compile_definitions(BOOST_PYTHON_STATIC_LIB)
参考:
Win10下CMakeList.txt配置使用Boost.python - 知乎 (zhihu.com)
如下(foo.py):
def greeting(s):
print("Get the param in Python: ", s, " \n")
return "Hello, I come from Pyhon world."
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})
#include // Boost.Python
#include
#include
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(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
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
需要设置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博客
Get the param in Python: Hello, I come from C++ world.
Get the result from python: Hello, I come from Pyhon world.