Compiling the C extensions in MinGW on Windows platforms with Anaconda - GeoscienceAustralia/tcrm GitHub Wiki

DEPRECATED December 2019

The C extensions are no longer used in the py3 branch, so this is no longer required. It is retained for guidance for those users still using V2.1 or older, and general reference (I've seen a lot of hits to this page, so must be of some use to someone!)

Background

When I moved to using Anaconda on my Windows machine, I had problems compiling the C extensions (Utilities.KPDF, Utilities.Cmap, Utilities.CStat and Utilities.akima). Previously, I'd used the MinGW gcc compiler without too many issues, and it's on this that I based the setup script (installer/setup.py and installer/setup.cfg). The extensions compiled using the setup script would not report an error when compiling, but when imported returned the following error:

ImportError: DLL load failed: The specified procedure could not be found.

Turns out that the MinGW compiler is somewhat 'temperamental' about the format of the Python export library.

This note is for those using Python 2.7, which may be superseded in the future.

Key steps:

  1. Install the MinGW compiler using the conda package manager:

conda install -c anaconda mingw

  1. Copy the Python DLL file (python27.dll) from the base directory of your anaconda installation to the libs sub-directory.
  2. Change directory into the libs directory.
  3. Use the gendef tool (supplied with the MinGW package) to create a list of the exports from the DLL:

gendef python27.dll

This will create a file python27.def. Alternative tools to do this are called dumpbin and pexports.exe (no links provided, since I didn't have to use them).

  1. Use the dlltool, supplied with MinGW, to create the required library file:

dlltool --dllname python27.dll --def python27.def --output-lib libpython27.a

  1. Now to the compilation of the extension modules. I had to compile manually to ensure the correct flags were used along the way. The following is for the KPDF module, but equally applies for the other modules. From the TCRM base directory (change /<path>/<to> to match the path to your installation of anaconda):

gcc -c Utilities/KPDF.c -I/<path>/<to>/anaconda/include -I/<path>/<to>/anaconda/Lin/site-packages/numpy/core/include/numpy -o Utilities/KPDF.o -Wall -O -std=c99 -D MSWIN64

gcc -shared -s Utilities.KPDF.o -L/<path>/<to>/anaconda/libs -lpython27 -o Utilities/KPDF.pyd

You should now have a Utilities/KPDF.pyd file.

  1. Check that it imports without error:

python -c "import Utilities.KPDF"

References:

  1. https://eli.thegreenplace.net/2008/06/28/compiling-python-extensions-with-distutils-and-mingw
  2. https://anaconda.org/anaconda/mingw

Craig Arthur November 2018

⚠️ **GitHub.com Fallback** ⚠️