【发布时间】:2021-08-06 08:03:28
【问题描述】:
我在使用简单的 boost python 设置时遇到了一些问题。 我见过很多其他人遇到过问题,但他们似乎都不是和我一样的问题,因为他们的解决方案都没有奏效。 作为参考,我在 Windows 10 上,使用 mingw64 10.2 作为我的 c++ 编译器的 msys2 的一部分。我使用该编译器构建了 boost 进行调试和优化,并且我还使用该编译器构建了一个链接 boost.python 的 dll。
我的 Cmake 文件:
cmake_minimum_required(VERSION 3.20)
project(test)
set(CMAKE_CXX_STANDARD 20)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
include_directories(${Python3_INCLUDE_DIRS})
set(Boost_ARCHITECTURE -x64)
set(Boost_NO_WARN_NEW_VERSIONS ON)
# set(Boost_DEBUG ON)
set(Boost_INCLUDE_DIR "C:/Devel/install/include/boost-1_76")
set(Boost_LIBRARY_DIR "C:/Devel/install/lib")
SET(Boost_USE_STATIC_LIBS OFF)
SET(Boost_USE_STATIC_RUNTIME OFF)
add_definitions("-DBOOST_ALL_DYN_LINK")
add_definitions("-DBOOST_UUID_USE_SSE2")
add_definitions("-DBOOST_UUID_USE_SSE3")
add_definitions("-DBOOST_PYTHON_STATIC_LIB")
SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "C:/Devel/install/include")
SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "C:/Devel/install/lib")
find_package(Boost 1.76.0 REQUIRED COMPONENTS python39)
include_directories(${Boost_INCLUDE_DIRS})
file(GLOB_RECURSE PythonBindings_SOURCES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/PythonBindings/*.cpp")
add_library(PythonBindings SHARED ${PythonBindings_SOURCES})
target_link_libraries(PythonBindings Boost::python39 Python3::Module)
set_target_properties(PythonBindings PROPERTIES LINKER_LANGUAGE CXX PREFIX "" SUFFIX ".pyd" IMPORT_PREFIX "" IMPORT_SUFFIX ".pyd.a")
target_compile_definitions(PythonBindings PRIVATE EXPORT_PYTHONBINDINGS)
Cpp 文件:
#include <boost/python.hpp>
char const*
helloWorld()
{
return "Hello, world!";
}
using namespace boost::python;
BOOST_PYTHON_MODULE(PythonBindings)
{
def("hello_world", helloWorld);
}
这成功编译为“PythonBindings.pyd”。在依赖walker中打开它,我可以看到它导出符号'PyInit_PythonBindings'
当我尝试从 python (python -vv py/helloworld.py) 使用这个 dll 时
import PythonBindings;
PythonBindings.hello_world()
我明白了:
Traceback (most recent call last):
File "C:\Devel\Working\test\py\helloworld.py", line 18, in <module>
import PythonBindings;
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
File "<frozen importlib._bootstrap>", line 565, in module_from_spec
File "<frozen importlib._bootstrap_external>", line 1173, in create_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
ImportError: DLL load failed while importing PythonBindings: The specified module could not be found.
我不确定问题是什么。我的 $PATH 变量包含 PythonBindings.pyd 的所有依赖项的搜索路径(libboost_python39-mgw10-mt-x64-1_76.dll、kernel32.dll、MSVCRT.dll、libgcc_s_seh-1.dll、libstdc++-6.dll、python39 .dll)
我将 dll 命名为与 python 模块相同,我知道有些人已经把它们绊倒了,我使用相同的 c++ 编译器和 python 版本来构建 boost 和我的库,我使用相同的 python 版本像我为运行我的 python 脚本所做的那样链接这两个。
我对如何解决这个问题一无所知。
【问题讨论】:
-
您可能想尝试ProcessMonitor 以了解使用 dll 的位置。在搜索路径上的其他 python 安装可能会导致问题。
标签: python c++ python-3.x boost-python