Tuesday, 15 September 2026

Making gridgen-c Buildable with MinGW on Windows

Grid generated by gridgen

Purpose

gridgen-c is a very useful library for generating two-dimensional orthogonal curvilinear grids. However, the original repository only supports building on Mac or Linux, and it cannot be built on Windows as is.
I have therefore modified it so that it can be built on Windows with MinGW, and I am publishing the result here.
I hope you find it useful.

Source Code

https://github.com/computational-sediment-hyd/gridgen-c_MinGW_Windows/

How to Build

Recommended: MSYS2 MinGW Shell

Install MSYS2, open the MSYS2 MinGW 64-bit shell, and run:

pacman -S --needed mingw-w64-x86_64-gcc make
cd gridgen
./configure
make
make lib      # libgridgen.a
make shlib    # libgridgen.dll (+ libgridgen.dll.a import library)

configure has already been regenerated with autoconf 2.69/2.71, so autoconf is not required.
Run autoconf -o configure configure.ac only if you edit configure.ac.

Without MSYS2 (plain cmd.exe + MinGW-w64)

A route that uses neither sh nor configure is also provided:

copy config.h.mingw config.h
mingw32-make -f makefile.mingw
mingw32-make -f makefile.mingw lib
mingw32-make -f makefile.mingw shlib

Source Code Changes (4 items)

Why the build fails

  1. triangle.c depends unconditionally on <sys/time.h>/gettimeofday()NO_TIMER is now defined automatically on Windows (on 32-bit x86, CPU86 is also defined automatically and x87 is fixed to 53-bit precision).
  2. The build system assumes Unix → configure.in was rewritten as configure.ac, with host detection via AC_CANONICAL_HOST. EXEEXT (.exe), SHLIB (dll), PICFLAG (empty), and --out-implib are switched accordingly. configure has been regenerated with autoconf 2.71 (autoconf not required).

Why it breaks after building (the main issue)

  1. LLP64 bug in Triangle — pointers are stored in unsigned long. On Win64, sizeof(long)==4, so every pointer loses its upper 32 bits and the program crashes immediately after startup (observed: page fault ... 0x00000000fe8d0050). The unsigned long used in pointer arithmetic was replaced with uintptr_t (41 locations).
  2. key_find() in gridgen.c relies on ftell() in text mode — on Windows text streams, ftell() does not return byte offsets (for the same LF file, Linux gives 11/25/31 while Windows gives 8/23/30). fseek lands in the middle of a key name, files with garbage names such as s sigmas.1 are created, and no grid is written (while the exit code is still 0). Fixed by opening the parameter file in binary mode, plus CRLF support.

For details of the changes, see here.
The diff is also available as a patch here.

Sample

I have confirmed that grids can be generated with the gridgen.exe built as described above.
The sample dataset is available here.

gridgen prm.0

Grid generated by gridgen

No comments:

Post a Comment

Making gridgen-c Buildable with MinGW on Windows

Purpose gridgen-c is a very useful library for generating two-dimensional orthogonal curvilinear grids. However, the original rep...