python - 操作系统错误 : exception: access violation reading with Cyphon basic example

标签 python c++ python-3.x anaconda cygwin

我只是按照 Calling C/C++ from Python? 上的简单教程进行操作, 你可以把原答案的代码复制粘贴过来,那我就放一张图来说明一下:

enter image description here

创建这些文件后,我构建了项目:

User@User-PC$ g++ --version (from Cygwin)
g++ (GCC) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

User@User-PC$ g++ -c -fPIC foo.cpp -o foo.o
User@User-PC$ g++ -shared -Wl,-soname,libfoo.so -o libfoo.so  foo.o

user@user-PC$ ll
total 162
drwx---r-x+ 1 user None      0 2019-05-15 06:59:29.850994400 -0300 ./
-rwx---r-x+ 1 user None    235 2019-05-15 06:59:02.791270500 -0300 foo.cpp*
-rw-r--r--+ 1 user None   3362 2019-05-15 06:58:54.335017300 -0300 foo.o
-rwx---r-x+ 1 user None    260 2019-05-15 06:59:40.159723600 -0300 foowrapper.py*
-rwxr-xr-x+ 1 user None 137902 2019-05-15 06:59:04.920040300 -0300 libfoo.so*

User@User-PC$ python3 foowrapper.py
Traceback (most recent call last):
  File "foowrapper.py", line 12, in <module>
    f.bar() #and you will see "Hello" on the screen
  File "foowrapper.py", line 9, in bar
    lib.Foo_bar(self.obj)
OSError: exception: access violation reading 0x000000636F6C6C61

User@User-PC$ python3 --version (from Windows) https://www.anaconda.com/distribution/
Python 3.7.2

User@User-PC$ python2 --version (from Cygwin)
Python 2.7.16

User@User-PCpython2 foowrapper.py
Hello

User@User-PC$

我正在使用 Cygwin g++ 构建它,我们可以看到,如果我使用 Windows Native Python 运行它,它会抛出异常 OSError: exception: access violation reading,但如果我使用 Cygwin Python,它就可以正常工作。

为什么 Windows Python 会抛出这样的异常?能否对其进行修复,以便我可以使用 Cygwin g++ 构建它并使用 Windows Native Python 编译器运行它?

最佳答案

这很可能是因为您在 Cygwin 中编译的代码依赖于 Cygwin DLL (cygwin1.dll)。只要该 DLL 在您的路径中,您仍然可以在 Windows 下运行它,但由于 Cygwin 处理路径的方式,可能会产生有趣的效果。如果要编译不依赖于 Cygwin DLL 的应用程序或 dll,则需要使用 Cygwin 中的 MinGW 编译器。你可能没有安装那些。重新运行安装程序并查找如下命名的包:mingw-x86[_64]-gcc-*。您仍将以类似的方式创建 DLL,但您将使用 gcc 的 mingw 版本。

关于python - 操作系统错误 : exception: access violation reading with Cyphon basic example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56147029/

相关文章:

python - python:如何在hdfs中读取.h5文件?

python - 使用 lxml 进行网页抓取不和谐,如何提取文本和照片链接?

c++ - 无法使用程序访问文件

python - multiprocessing.Queue 作为池工作程序的参数中止工作程序的执行

Python 将数据集中的相似记录(字符串)分组

c++ - 带有 For 循环和指针操作的 OPENMP

c++ - 无法对数组中的元素重新排序 - RapidJSON

python-3.x - 从 sklearn 导入数据集时出错

python - 多线程时循环不使用新变量运行

python - ElementTree 在 Python 中解析下一个标签