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:
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?