网站经常被黑,网上店铺怎么开通,平面设计风格有哪些类型,广州深圳做网站这个过程有挺多坑#xff0c;记录一下。我这里的环境#xff1a;
Windows 11
Qt 6.2
Boost 1.8.4
CMake 3.25.2
Visual Stutio 2019#xff08;主要用于C编译#xff09;
1、下载并将Boost编译为静态库 b2.exe toolsetmsvc-14.2 install --prefixboost安装路径 links…
这个过程有挺多坑记录一下。我这里的环境
Windows 11
Qt 6.2
Boost 1.8.4
CMake 3.25.2
Visual Stutio 2019主要用于C编译
1、下载并将Boost编译为静态库 b2.exe toolsetmsvc-14.2 install --prefixboost安装路径 linkstatic 参考
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 stringnamespace pyboost::python;int main(int argc, char* argv[])
{Py_Initialize(); // 初始化 Python 解释器try{//导入模块 hellohello.pypy::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::extractstd::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 PYTHONHOMEPython环境根目录 否则C Boost调用python报错 init_fs_encodingfailed to get the Python codec of the file 原因是python的环境变量没有配置好。 set PYTHONHOMEPython环境根目录 参考
【BUG】C Boost调用python报错init_fs_encodingfailed 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.