I wan't want to create a cross-plattform programm that embedds the python interpreter, and compile it with MinGW. But the Python Binary distribution provides no libraries for MinGW to link with (only python32.lib
for Visual C++), and the Python Source package provides no support for compiling with MinGW.
I tried linking to python32.lib
in Mingw with -lpython32
but it still generates errors like:
main.cpp: undefined reference to `_imp__Py_Initialize'
main.cpp: undefined reference to `_imp__Py_Finalize'
How do I link Python in MinGW? I really don't want to switch to using Visual C++.
Try this...
gendef /c/windows/system32/python32.dll
dlltool -D python32.dll -d python32.def -l libpython32.a
libpython32.a
to your./python32/libs
directory.If your
libpython32.a
file is 0 bytes, something went wrong. Double-check that you downloaded the correct version of gendef for your version of mingw/msys. If you're running a 64-bit build, you'll likely have to download the gendef binaries and compile yourself, but that's simple enough.Hope that helps.
EmbeddingPython.c
and use gcc as following:
and it works as we expected.
With nm and dlltool from binutils, you should be able to rebuild the library for gcc:
python_test.c:
Compile:
Test:
Edit: If you'd prefer to skip building this yourself on x64, you can download it for several versions from Christoph Gohlke's Unofficial Windows Binaries for Python Extension Packages.
Edit: Here's a Python version based on the existing function that's distributed in Tools/msi/msi.py:
For example: