I am trying to call a C++ function from python. I tried this example but don't get it working.
When I try to compile in cmd.exe on Windows: g++ -shared -c -fPIC function.cpp -o function.o
, I get a warning: -fPIC ignored for target (all code is position independent). I don't know whether this is an important flag in the first place?
My function.o appears, and I compile: g++ -shared -Wl,-soname,library.so -o library.so function.o
to make it a shared library. The file library.so appears.
Then I run my wrapper:
import ctypes
print ctypes.windll.library.square(4)
Upon which this error appears:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site- packages\spyderlib\widgets\externalshell\sitecustomize.py", line 523, in runfile
execfile(filename, namespace)
File "C:\Users\Renger\untitled14.py", line 2, in <module>
print ctypes.windll.library.square(4) # windows
File "C:\Python27\lib\ctypes\__init__.py", line 435, in __getattr__
dll = self._dlltype(name)
File "C:\Python27\lib\ctypes\__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] Can't find given module
My python program and the library.so file are saved in the same directory.
Am I missing something obvious here?
Best!