0

I just followed the simple tutorial on Calling C/C++ from Python?, you can copy paste the code from the original answer over there, then I am just putting here a picture for illustration:

enter image description here

After creating these files, I built the project with:

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$

I am building it with Cygwin g++ and we can see that if I run it with Windows Native Python, it throws the exception OSError: exception: access violation reading, but if I use a Cygwin Python, it works just fine.

Why the Windows Python is throwing such exception? Can it be fixed, so I can build it with Cygwin g++ and run it with Windows Native Python compiler?

Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

1 Answers1

1

Most likely this is because your code compiled in Cygwin depends upon the Cygwin DLL (cygwin1.dll). You can still run that under Windows as long as that DLL is in your path, but there may be interesting effects due to how Cygwin treats paths. If you want to compile an application or dll that does not depend on the Cygwin DLL, you'll need to use the MinGW compilers in Cygwin. You probably don't have those installed. Re-run setup and look for packages named like the following: mingw-x86[_64]-gcc-*. You'll still create your DLL in a similar fashion, but you'll use the mingw version of gcc.

SeeJayBee
  • 1,188
  • 1
  • 8
  • 22