Skip to main content
 首页 » 编程设计

python之在 Cygwin 中使用 Ctypes 将 DLL 加载到 Python 中的奇怪问题

2024年10月01日37sxdcgaq8080

我已经在这里工作了几个小时。我有一个简单的 hello-world .dll 使用 Clion 在 Cygwin 上使用 gcc 编译。

我在位于 Cygwin 目录的 Pycharm 中从 anaconda 安装(即解释器)运行了一个 Python 脚本。

.dll 文件名为“cygeextension_test_c.dll”。


我将 .dll 移动到 python 项目目录,并在下面列出的各种配置中运行以下代码(所有这些都是我今天下午可以找到的关于这个主题的过去十年的各种解决方案——我正在使用以下 website 将 ndarrays 发送到 C 代码):

import numpy as N 
from numpy.ctypeslib import load_library 
from numpyctypes import c_ndarray 
import ctypes 
import os 
 
print(ctypes.windll.kernel32); 
 
mydll = load_library('cygextension_test_c.dll',r'C:\cygwin64\home\...\extension_test_c\cmake-build-debug'); 
 
mydll = ctypes.WinDLL(r'same path, essentially--this time pointing to working directory\cygextension_test_c.dll') 
 
mydll = ctypes.CDLL(r'ditto') 
 
mydll = ctypes.cdll.LoadLibrary('...') 
 
mydll = ctypes.windl.LoadLibrary('...') 
 
myarray = N.zeros((3,13),dtype=N.double) 
c_myarray = c_ndarray(myarray,dtype=N.double,ndim=2) 

我还单步执行了代码,并注意到正在访问路径和文件。如果我将路径更改为不正确的内容(或文件名等),我会在链的前面得到一个不同的错误。这在某种程度上属于 .dll 本身。然而,几乎所有的问题都得到了(显然)与我得到的相同的错误,并且它与他们案例中的实际 .dll 无关。他们只是通过切换使用 ctypes 导入 .dll 的方式来获得神奇的解决方案。


因此,我再次在(显然)Windows 计算机上的 Cygwin 目录中的 pycharm 上使用 Windows anaconda 安装(python 3.5)。 c++ 代码是使用 Clion 中的 gcc 编译的,在 CMakeLists 文件中使用以下设置(按照上面链接中的说明):

cmake_minimum_required(VERSION 3.6) 
project(extension_test_c) 
 
set(CMAKE_CXX_STANDARD 11) 
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -ansi -pedantic -Wall -pedantic -std=c++0x") 
 
set(SOURCE_FILES extension_test_c.cpp extension_test_c.h ndarray.h) 
add_library(extension_test_c SHARED ${SOURCE_FILES}) 

我在 extension_test_c.cpp 中编译的代码只是一个函数,它打印出 stride 数组在二维 numpy ndarray 中有哪些数字(这与链接中的代码有关),标题中的代码更简单;但是,我已经注释掉了 std 行,因为我遇到了另一个关于库是否访问其他库的早期问题的答案(因为这可能导致 ctypes 导入失败)。

extension_test_c.cpp

#include "extension_test_c.h" 
 
//#include <iostream> 
#include "ndarray.h" 
 
extern "C" { 
int hello(numpyArray<double> array) { 
    Ndarray<double,2> a(array); 
 
    //std::cout << "shape 0: " << array.shape[0] << "; shape 1: " << 
    //          array.shape[1] << "; stride 0: " << array.strides[0] 
    //          << "; strides 1: " << array.strides[1] << std::endl; 
    return 1;  
} 
} 

和头文件:

#ifndef EXTENSION_TEST_C_LIBRARY_H 
#define EXTENSION_TEST_C_LIBRARY_H 
 
#include "ndarray.h" 
extern "C" { 
int hello(numpyArray<double> array);  
} 
#endif 

ndarray.h 可以在上面网站的链接中找到。


我不知道自己做错了什么;我完全没有想法。这是 load_library 的错误:

  Traceback (most recent call last): 
  File "C:/cygwin64/home/chris/CygwinMachineLearning/Assignment 1/DecisionTree/extension_test_python/wrapper.py", line 9, in <module> 
      mydll = load_library('cygextension_test_c.dll',r'C:\cygwin64\home\chris\CygwinMachineLearning\Assignment 1\DecisionTree\extension_test_c\cmake-build-debug'); 
  File "C:\Anaconda3\envs\tensorflow\lib\site-packages\numpy\ctypeslib.py", line 150, in load_library 
      return ctypes.cdll[libpath] 
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 422, in __getitem__ 
      return getattr(self, name) 
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 417, in __getattr__ 
      dll = self._dlltype(name) 
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 347, in __init__ 
      self._handle = _dlopen(self._name, mode) 
OSError: [WinError 126] The specified module could not be found 

ctypes.CDLL(...) 的错误:

Traceback (most recent call last): 
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevd.py", line 1596, in <module> 
    globals = debugger.run(setup['file'], None, None, is_module) 
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevd.py", line 974, in run  
    pydev_imports.execfile(file, globals, locals)  # execute the script 
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile 
    exec(compile(contents+"\n", file, 'exec'), glob, loc) 
  File "C:/cygwin64/home/chris/CygwinMachineLearning/Assignment 1/DecisionTree/extension_test_python/wrapper.py", line 9, in <module> 
    mydll = ctypes.CDLL('cygextension_test_c.dll',r'C:\cygwin64\home\chris\CygwinMachineLearning\Assignment 1\DecisionTree\extension_test_c\cmake-build-debug'); 
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 347, in __init__ 
    self._handle = _dlopen(self._name, mode) 
OSError: [WinError 126] The specified module could not be found 

而且,对于WinDLL,也是一样的。


对于 cdll.LoadLibrary 版本,它几乎相同(完全相同的最终错误),但跟踪不同:

Traceback (most recent call last): 
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevd.py", line 1596, in <module> 
    globals = debugger.run(setup['file'], None, None, is_module) 
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\pydevd.py", line 974, in run 
    pydev_imports.execfile(file, globals, locals)  # execute the script 
  File "C:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile 
    exec(compile(contents+"\n", file, 'exec'), glob, loc) 
  File "C:/cygwin64/home/chris/CygwinMachineLearning/Assignment 1/DecisionTree/extension_test_python/wrapper.py", line 9, in <module> 
    mydll = ctypes.cdll.LoadLibrary(r'C:\cygwin64\home\chris\CygwinMachineLearning\Assignment 1\DecisionTree\extension_test_c\cmake-build-debug'); 
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 425, in LoadLibrary 
    return self._dlltype(name) 
  File "C:\Anaconda3\envs\tensorflow\lib\ctypes\__init__.py", line 347, in __init__ 
    self._handle = _dlopen(self._name, mode) 
OSError: [WinError 126] The specified module could not be found 

请您参考如下方法:

经过几个小时的尝试,我的解决方案是将“-static”添加到 gcc 链接器命令中。来自Gcc Link Options :

-static

On systems that support dynamic linking, this overrides -pie and prevents linking with the shared libraries. On other systems, this option has no effect.

[WinError 126] 提示无法找到某些相关的 dll。提示是在不同的 Windows 系统上加载相同的 dll 时。安装 WSL 也可能使其工作,但静态链接为没有 WSL 的机器修复了它。