I took a very simple Python C API example off the web. When I remove #include <iostream>
, I can properly import it into Python. When I keep iostream
in and try to import the DLL, Python gives me this error: ImportError: DLL load failed while importing fib: The specified module could not be found.
I am using Python 3.10.
Note: the error is not because Python could not find fib.pyd or PyInit_fib
#include <Python.h>
#include <iostream>
int _fib(int n)
{
//unimportant implementation details
return 0;
}
static PyObject* fib(PyObject* self, PyObject* args)
{
int n;
return Py_BuildValue("i", _fib(n));
}
static PyMethodDef FibMethods[] = {
{"fib", fib, METH_VARARGS, "Calculate the Fibonacci numbers (in C)."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef ticlib={
PyModuleDef_HEAD_INIT,
"fib",
"A fast tic tac toe implementation",
-1,
FibMethods,
NULL,
NULL,
NULL,
NULL
};
PyMODINIT_FUNC
PyInit_fib(void)
{
using namespace std;
return PyModule_Create(&ticlib);
}
It could possible be related to the dependencies of fib.pyd.
Broken version of fib.dll (includes iostream)
$ ldd fib.dll
ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffb9f9d0000)
KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ffb9f1f0000)
KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ffb9d1c0000)
msvcrt.dll => /cygdrive/c/WINDOWS/System32/msvcrt.dll (0x7ffb9ed00000)
libstdc++-6.dll => /cygdrive/c/Program Files (x86)/GtkSharp/2.12/bin/libstdc++-6.dll (0x6fc40000)
python310.dll => /cygdrive/e/Python/Python310/python310.dll (0x7ffb2e400000)
WS2_32.dll => /cygdrive/c/WINDOWS/System32/WS2_32.dll (0x7ffb9f0f0000)
VERSION.dll => /cygdrive/c/WINDOWS/SYSTEM32/VERSION.dll (0x7ffb95e30000)
RPCRT4.dll => /cygdrive/c/WINDOWS/System32/RPCRT4.dll (0x7ffb9f800000)
libstdc++-6.dll => /cygdrive/e/msys64/mingw64/bin/libstdc++-6.dll (0x7ffb2de10000)
ADVAPI32.dll => /cygdrive/c/WINDOWS/System32/ADVAPI32.dll (0x7ffb9db40000)
sechost.dll => /cygdrive/c/WINDOWS/System32/sechost.dll (0x7ffb9f040000)
ucrtbase.dll => /cygdrive/c/WINDOWS/System32/ucrtbase.dll (0x7ffb9d550000)
VCRUNTIME140.dll => /cygdrive/c/WINDOWS/SYSTEM32/VCRUNTIME140.dll (0x7ffb84c50000)
libgcc_s_seh-1.dll => /cygdrive/e/msys64/mingw64/bin/libgcc_s_seh-1.dll (0x7ffb98030000)
libwinpthread-1.dll => /cygdrive/e/msys64/mingw64/bin/libwinpthread-1.dll (0x7ffb93370000)
Working version of fib.dll (does not include iostream)
$ ldd fib.dll
ntdll.dll => /cygdrive/c/WINDOWS/SYSTEM32/ntdll.dll (0x7ffb9f9d0000)
KERNEL32.DLL => /cygdrive/c/WINDOWS/System32/KERNEL32.DLL (0x7ffb9f1f0000)
KERNELBASE.dll => /cygdrive/c/WINDOWS/System32/KERNELBASE.dll (0x7ffb9d1c0000)
msvcrt.dll => /cygdrive/c/WINDOWS/System32/msvcrt.dll (0x7ffb9ed00000)
python310.dll => /cygdrive/e/Python/Python310/python310.dll (0x7ffb2e400000)
WS2_32.dll => /cygdrive/c/WINDOWS/System32/WS2_32.dll (0x7ffb9f0f0000)
RPCRT4.dll => /cygdrive/c/WINDOWS/System32/RPCRT4.dll (0x7ffb9f800000)
VERSION.dll => /cygdrive/c/WINDOWS/SYSTEM32/VERSION.dll (0x7ffb95e30000)
ADVAPI32.dll => /cygdrive/c/WINDOWS/System32/ADVAPI32.dll (0x7ffb9db40000)
sechost.dll => /cygdrive/c/WINDOWS/System32/sechost.dll (0x7ffb9f040000)
ucrtbase.dll => /cygdrive/c/WINDOWS/System32/ucrtbase.dll (0x7ffb9d550000)
VCRUNTIME140.dll => /cygdrive/c/WINDOWS/SYSTEM32/VCRUNTIME140.dll (0x7ffb84c50000)