OSError: exception: access violation reading with Cyphon basic example

OSError: exception: access violation reading with Cyphon basic example

我只是按照Calling C/C++ from Python?上的简单教程,你可以复制粘贴那里原来答案的代码,然后我只是放一张图来说明:

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

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 本机 Python,它会抛出异常 OSError: exception: access violation reading,但如果我使用 Cygwin Python,它工作得很好。

为什么 Windows Python 抛出这样的异常?它可以修复吗,所以我可以用 Cygwin g++ 和 运行 用 Windows 本机 Python 编译器构建它?

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