I'm trying to speed up parts of my python code by calling c++ code using ctypes. I found many step-by-step guides, including some on this website. However, basically all of them assume linux operating systems and I am working with windows.
Based on some of the linux examples and some googling, I created the following MWE:
myfunc.cpp
extern "C"
int timestwo(int mynum){
return mynum*2;
}
I compile this with:
gcc -shared -o myfunc.dll myfunc.cpp
Then, I try to load the DLL file with python using:
usefunc.py
import ctypes
from os.path import abspath
dll = ctypes.CDLL(abspath('myfunc.dll'))
However, this gives the following error message:
OSError: [WinError 193] %1 is not a valid Win32 application
Edit: Solved. The problem was that I was generating a 32 bit DLL while using 64 BIT python. I re-installed MINGW64 with the following settings:
Architecture: x86_64
Threads: Win32
Exception: Seh
This solved the problem.