[英]boost.python “DLL load failed” on WinXP, VisualStudio 2005
[英]Python failing to load boost.python dll
我在使用簡單的提升 python 設置時遇到了一些問題。 我見過很多其他人遇到過問題,但他們似乎都不是和我一樣的問題,因為他們的解決方案都沒有奏效。 作為參考,我在 windows 10 上,使用 mingw64 10.2 作為我的 c++ 編譯器的 msys2 的一部分。 我使用該編譯器構建了 boost 進行調試和優化,並構建了一個 dll 鏈接到 boost.python 與該編譯器。
我的 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)
cp文件:
#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 的 dll 時( python -vv py/helloworld.py
)
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.
我不確定問題是什么。 My $PATH variable contains the search paths for all the dependencies of PythonBindings.pyd (libboost_python39-mgw10-mt-x64-1_76.dll, kernel32.dll, MSVCRT.dll, libgcc_s_seh-1.dll, libstdc++-6.dll, python39 .dll)
I named the dll the same as the python module, which I know some people have had trip them up, and I used the same c++ compiler and python version for building boost and my library, and I used the same python version for linking both of就像我為運行我的 python 腳本所做的那樣。
我對如何解決這個問題一無所知。
我按照 doqtor 的評論按照這里的教程進行操作,發現問題是 python 沒有加載幾個作為 PythonBindings.pyd 運行時依賴項的 dll。
這是通過添加修復的
import sys
import os
[os.add_dll_directory(dir) for dir in sys.path if os.path.isdir(dir)]
在import PythonBindings
之前
我很奇怪 python 沒有在 sys.path 中搜索 dll 依賴項。 它需要搜索的路徑也在 os.environ['PATH'] 上,但它再次沒有搜索它們。 我不確定是否有更好的方法來做到這一點。 如果有的話,我想聽聽。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.